matlab - creating a legend inside loop -
i'm trying legend label multiple plots generated inside loop. i've read several feeds on topic. i'm not sure why seem unclear, still can't figure out way work.
x=0:.2:13; y=zeros(4,length(x)); slope=zeros(1,4); strings=zeros(1,4); hold on grid on cnt=1:4 slope(cnt)=-omega(cnt)/trq(cnt); y(cnt,:)=x*slope(cnt)+omega(cnt); plot(x,y(cnt,:)) str=sprintf('%f volts',v(cnt)); legend(str) end axis([0 .05 0 300]) i've tried moving legend command outside loop, , i've tried making array of strings inside loop. suggestions welcome.
the function legend expects collection of strings whereas passing in 1 string value on each call. therefore, should accumulate strings want legends within loop , call legend outside of loop collection.
str = cell(1,4); %initialize cell array hold legend strings . . . str{cnt}=sprintf('%f volts',v(cnt)); %insert text appropriate cells end legend(str)
Comments
Post a Comment