legend imformation is incorrect

Please I am plotting the data attached with just the simple code below but the legends repeats the first color for the second plot.
plot(DownExtraction','m');
hold on
plot(Upperextraction','b');
xlabel('Wavelength');
ylabel('Intensity');
legend('Down Extraction','Upper Extraction');
title('FEMD Dual Position Extraction of DS');
Please your help wil be appreciated.

 Accepted Answer

You must be plotting something else that is not in the code you posted or the data you attached.
This code:
D1 = load('Upper extraction.mat');
UpperExtraction = D1.DS_DATA_ODU;
D2 = load('Down Extraction.mat');
DownExtraction = D2.DS_DATA_ODD;
plot(DownExtraction','m');
hold on
plot(UpperExtraction','b');
xlabel('Wavelength');
ylabel('Intensity');
legend('Down Extraction','Upper Extraction');
title('FEMD Dual Position Extraction of DS');
produces this plot:
That appears correct to me.
Note that the line at zero intensity does not appear.
The problem may be that you are plotting something else afterwards, with the hold state still on.
Try this instead:
figure
plot(DownExtraction','m');
hold on
plot(UpperExtraction','b');
hold off % <— Toggle ‘hold’ Off Here
xlabel('Wavelength');
ylabel('Intensity');
legend('Down Extraction','Upper Extraction');
title('FEMD Dual Position Extraction of DS');
Experiment to get the result you want.

11 Comments

this really helped im sincerely grateful.
The first figure I sent is the correct figure. is there a way I can copy the legend of the second figure you sent into the first figure.
As always, my pleasure!
The first figure I sent is the correct figure. is there a way I can copy the legend of the second figure you sent into the first figure.
I am not quite certain what you want.
Try this:
figure
hp{1} = plot(DownExtraction','m');
hold on
hp{2} = plot(UpperExtraction','b');
hold off % <— Toggle ‘hold’ Off Here
xlabel('Wavelength');
ylabel('Intensity');
legend([hp{:}], 'Down Extraction','Upper Extraction');
title('FEMD Dual Position Extraction of DS');
You can then plot anything else you want. Only the ‘hp’ plot colours will be present in the legend.
Experiment to get the result you want.
The code I posted works for me (in R20198b). I can’t tell what is throwing the horzcat error.
Try this slightly edited version:
figure
hp1 = plot(DownExtraction','m');
hold on
hp2 = plot(UpperExtraction','b');
hold off % <— Toggle ‘hold’ Off Here
xlabel('Wavelength');
ylabel('Intensity');
legend([hp1 hp2], 'Down Extraction','Upper Extraction');
title('FEMD Dual Position Extraction of DS');
Please Im grateful. As far it works on your version of your matlab.Its ok. I will get that version. I just tried but same error still occured. Im using (R2015a)
Please can attach me the figure that worked on your version.
thank you.
As always, my pleasure.
What line is throwing the error? What does it refer to? (Please include all the red text from your Command Window.)
Please can attach me the figure that worked on your version.
I already did. It is the figure in my original Answer.
My code should work in R2015a. It might be worthwhile to attach all the data that you used to plot all your data, and the code you wrote to plot them. (I no longer have access to R2015a, so I cannot test my code with it.)
sorry for the delay. Unfortunaately I left the lab for few days.
I am grateful for your support and concern.
The data for the plot I first posted in my question is the data I am working with at the moment in my current research. The data you used in your answer is the original data but contains error of noise and scattering component as you can see in your plot.
Literally what I want to achieve is the plot of the initial data I posted in the question with it correct legend.
Attached is the data for the original data and the current data Im trying to plot.
The down Extraction and Upper extraction is the current data Im trying to plot. And the remaining two DS data is the original data as used in your plot.
I will be humbled for your support.
It worked fine for me in R2015a using the commands:
D1 = load('Upper extraction.mat');
UpperExtraction = D1.DS_DATA_ODU;
D2 = load('Down Extraction.mat');
DownExtraction = D2.DS_DATA_ODD;
>> figure
hp{1} = plot(DownExtraction','m');
hold on
>> hp{2} = plot(UpperExtraction','b');
>> hold off % <— Toggle ‘hold’ Off Here
xlabel('Wavelength');
ylabel('Intensity');
legend([hp{:}], 'Down Extraction','Upper Extraction');
But literally that is the plot for the original data( DS_DATA_ODU and DS_DATA_ODD).
The data i want to plot is the data attached.
The code I provided reads from the Upper and Down .mat files and plots their contents. It is not reading from DS_DATA_ODD2.mat or DS_DATA_ODU2.mat . The variables it loads are the only things in the Upper and Down .mat files, so the code does plot what you ask to be plotted.
If you want to plot additional lines, then record their handles and pass the appropriate handles to legend() .
It looks likely to me that you are plotting four lines, with the first two being the same color and the same place as each other, thus giving the impression that only one line has been plotted.
I am grateful for kindness and support.
I hope you can try plotting the attached data. May be the previous data was same as the original data.
I used the code below but got the attached error.
figure
hp{1} = plot(Down','m');
hold on
hp{2} = plot(Upper','b');
hold off % <— Toggle ‘hold’ Off Here
xlabel('Wavelength');
ylabel('Intensity');
legend([hp{:}], 'Down Extraction','Upper Extraction');
title('FEMD Dual Position Extraction of DS');
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
ccc.png
@Walter — Thank you!
@Yussif Moro Awelisah — Your data are confusing and not well documented.
This worked for me when I tried it (with the ‘Down.mat’ and ‘Upper.mat’ files attached to your latest Comment):
figure
hp1 = plot(Down','m');
hold on
hp2 = plot(Upper','b');
hold off % <— Toggle ‘hold’ Off Here
xlabel('Wavelength');
ylabel('Intensity');
legend([hp1(1),hp2(1)], 'Down Extraction','Upper Extraction');
title('FEMD Dual Position Extraction of DS');
You need to experiment with the ‘hp’ handles (as I have called them here) to be sure they correspond with the data you are plotting. I have no idea what they are or how you calculated them, so I just experimented until I got them to work. If they are the reverse of what they should be, reverse the order of the ‘hp’ elements in the legend handle vector (between the square brackets []).
Note that ‘Down’ is a (6x500) double matrix, and ‘Upper’ is a (4x500) double matrix. You appear to be plotting the same data (or approximately the same data) several times.

Sign in to comment.

More Answers (1)

@Star Strider
@Walter Roberson
I am sincerely grateful. This worked for me and I am sincerely grateful for your loyalty and kindness.

Tags

Community Treasure Hunt

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

Start Hunting!