Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with legend

Status
Not open for further replies.

junkie26

Chemical
Mar 31, 2005
6
NL
I have a for loop which opens different ascii-files, does some calculations on them and the plots one column against another one from each file, and it does this for the files with numbers ifile to nfile. So I get 1 figure with various plots. Now I want to add a legend with the plotted lines, which just showes the numbers of the files.

This is part of my code:

-------------------------------------------------
infile='ascii';
ifile=1;
nfile=4;
k=1;

for icount=ifile:nfile,
mx(k)=num2str(ifile);
mx_2=mx'
k=k+1;

***here are some calculations and defenition of x_value and y_value***

hold all
plot(x_value,y_value)

ifile=ifile+1;
legend(mx_2);
hold off
end
-------------------------------------------------

So I want to use the vector mx_2 as the legend. If nfile is 4, then I plot 4 lines in one figure, and mx_2 is a vertical vector of 4 strings. However, I do get the following error. WHAT AM I DOING WRONG???

??? Error using ==> strcmp
The number of rows of the string matrix must
match the number of elements in the cell.

Error in ==> legend at 153
elseif (narg==1 && ischar(varargin{1}) && ...
 
Replies continue below

Recommended for you

buildString = int2str( ifile )

for...

...
eval( [ 'lengend( ', buildString, ')' ] );
buildString = [ buildString, ', ', int2str( icount ) ];
...

end
 
Hm, with that code I'm getting an error which I've seen before:

??? Error using ==> legend
Handle must be an axes or child of an axes.

Error in ==> tensile_tests_multiple_files at 66
eval( [ 'legend( ', buildString, ')' ] );

What's wrong!?
 
buildString = [ '''', int2str( ifile ), '''' ]

for...

buildString = [ buildString, ', ', '''', int2str( icount ), '''' ];
eval( [ 'legend( ', buildString, ')' ] )

end
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top