×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Problem with legend

Problem with legend

Problem with legend

(OP)
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}) && ...

RE: Problem with legend

buildString = int2str( ifile )

for...

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

end

RE: Problem with legend

(OP)
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!?

RE: Problem with legend

buildString = [ '''', int2str( ifile ), '''' ]

for...

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

end

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources