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

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

1) I don't follow exactly what you mean w/ "two separate graphs on the same figure" but I'll take a stab at it based on a supposition regarding the legend...
Instead of what I presume is a case that basically boils down to something similar to what would be
plot(x,y1)
legend(...
hold on
plot(x,y2)
In one sequence, the simple answer for a combined legend is to rather just
plot(x,[y1 y2])
legend(...
where the legend list is sufficient for the number of columns total.
2. Create dynamic legends w/ num2str() or sprintf() --
l=sprintf('frequency= %.1f',f);
legend(l)
For multiple curves, build a cellstr() array.
Im not familiar with sprintf and num2str but ill read up on it. Ill clarify my question 1. Lets say I have two m files, file1 and file2. So I run both and the output are plots and I use the hold command to display both on the same figure. In file1 the output is
plot(x1,y1)
leg1=legend(frequency1)
and in file2 the output is
plot(x2,y2)
leg2=legend(frequency2)
Then in the matlab terminal I run
file1(10,20)
hold on
file2(30,40)
So rather than specifying the legend as
legend(leg1,leg2)
is there a command which does this automatically? I need this because it leads onto my second questions where I need to include an input value into every legend and would rather not have to write them out manually.
I hope my question is clearer this time.
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
Sorry for the late post but this actually solved my problem. Thank you!

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!