Color of legend doesnt fit the plot

1 view (last 30 days)
Tatjana Mü
Tatjana Mü on 2 May 2022
Commented: Tatjana Mü on 3 May 2022
Dear all,
I know this question was asked thousand times before. But none of the solutions are working for me. I have a loop to import several files. Afterwards I do some calculations and want to plot them (for all the files in one plot). Therefore I use following code:
directory_name=uigetdir('','Ordner mit Messungen auswählen');
[nur_file_name,pfad]=uigetfile({'*.csv','csv-files (*.csv)';'*.*','all Files'},...
'Die csv-Files der Proben oeffnen (probe_001.csv=',[directory_name '/'], 'Multiselect', 'on');
nur_file_name=cellstr(nur_file_name);
nur_file_name=sort(nur_file_name);
filename=strcat(pfad,nur_file_name);
anzahl_files=size(filename,2);
for xy=1:anzahl_files
fid_in=fopen(char(filename(xy)),'r');
%Calculations...
%Plot Sr ratio for all files:
[NumRows1 NumCols1]=size(Ratio);
figure(1)
title('Proben 87Sr/86Sr');
xlabel('Partikel');
ylabel('87Sr/86Sr');
for p=1:anzahl_files
h(p)= scatter(1:NumRows1, Ratio(:,1));
legendtext{p} = filename_s(1,p);
hold on
end
legend(h, legendtext)
end
I am sorry to ask this again. But I dont find my mistake. It plots perfectly, the probleme is just that the colours are not matching. I appreciate any help.

Answers (1)

Image Analyst
Image Analyst on 2 May 2022
Why are you plotting the same data (the first column of Ratio) every iteration? If you do that, all the markers are on top of each other so that only the last set will show up. Did you mean to scatter different columns of Ratio like this:
h(p)= scatter(1:NumRows1, Ratio(:, p));
  9 Comments
Image Analyst
Image Analyst on 3 May 2022
Your indenting is all messed up. Looks like you have a for loop with 5 other for loops inside of it. Is that what you want?
Type control-a (to select all) then control-i (to fix the indenting).
Tatjana Mü
Tatjana Mü on 3 May 2022
@Image Analyst I am sorry for the messed up part. I am honestly kind of new in programming. I am mostly if it works like I want it to work. And mostly it just works like I want to have it, if I use a lot of loops.
Thank you for explaining the control a/ control i to me.
Do you maybe know why my legend is not working?

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!