Combining legends from multiple m files, and including an input variable in the legend.

5 views (last 30 days)
Hi this is my problem its actually two questions.
1. I have two m files plotting two separate graphs on the same figure. I have a legend assigned in each m file. The line colours and styles are different. Without writing a specific legend command for the two graphs in the matlab terminal or in one of the m files, how do I tell matlab to automatically combine the two legends?
2. One of my inputs is a frequency which I would like to display in the legend. I dont want to have to manually add the frequency into the legend in the m file for every frequency plot. So for example lets say the m file is file1(freq,length) and I run file1(10,4). So in the legend I have labeled the line 'frequency = freq ' and I want that input 'freq' value to automatically display there. How do I automatically have matlab add the frequency value to the legend?
I hope I am clear enough, if not I can explain more.
Thank you in advance for reading.
  4 Comments
dpb
dpb on 19 Oct 2013
Nothing happens automagically, no...
You'll need some way to generate the values of the legend for each plot -- if it's the input to the two functions then I've shown you how to manipulate it.
But, sound like what you really need to do is to write another function that calls the two others and is the one that you then run from the command line that calls the two other functions. IOW, basically put the commands you've listed above into another script and execute it instead of doing it all manually.
Either have it ask for the required inputs or use variables already defined in the workspace--I'll illustrate the latter.
In workspace type
x1=10; y1=20;
x2=30; y2=40;
f=[1; 2]; % NB column vector
toplevel.m % contents
file1(x1,y1)
hold on
file2(x2,y2)
legend(num2str(f,'freq %.1f')))
Then at the command line set the variables and just execute
toplevel
All the stuff other than just setting the values is then hidden. The point is to organize the repetitive stuff into another script or function that does the work you've been doing manually each time

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!