Issues in legend color lines in for loop

Hi,
Each iteration I am plotting the file has many arrays but legend granted a single line with the end of the file name otherwise same color for all the iteration files.
I made a color array based on my iteration and same for the legend
the attached graph will show whats the exact problem in legend
%% Start
clear all; close all; clc;
%%
D = uigetdir(pwd,'Select Directory');
S = dir(fullfile(D,'*.csv'));
file_list_1 = dir(fullfile(D, '*Peak + Bottom*.csv'));
CM = jet(length(file_list_1)/2); %for colouring
CM2=kron(CM,ones(2,1));%for duplicate the same colour in next next rows
%% Get the legend names from file list and remove the .csv and remove unwanted strings
legendTitle = cell(1,length(file_list_1));
for i = 1:length(file_list_1)
file=file_list_1(i).name;
%Your other operations
[~,fileName,~] = fileparts(file); % e.g., file is 'dp600_2_layers_L50.xlsx'
legendTitle{1,i} = fileName;
end
Legend_Modified=cellfun(@(x) x(20:min(end, 30)), legendTitle, 'UniformOutput', false); %remove unwanted strings
Legend_Modified=Legend_Modified';
%% Plot the results
for i=1:length(file_list_1)
file_name=file_list_1(i).name;
[x,y]=folder2array_V2(D, file_name);
xnew=ones(size(y)).*x;
xprime=xnew';
yprime=y';
plot(xprime,yprime,'--','LineWidth',2,'color',CM2(i,:)); hold on % plot the each file's bulk array
end
legend(Legend_Modified);
N_units=size(y,1);
untitled.jpg

3 Commenti

Insufficient info but..
What is value of
numel(file_list_1)
?
I'm guessing based on the logic and comment re:
Legend_Modified=cellfun(@(x) x(20:min(end, 30)), legendTitle, 'UniformOutput', false); %remove unwanted strings
it's > than the 12 strings that are showing in the label and there are those additional lines plotted that aren't visible because later iterations drew over them with revised colors--but legend() just labels the first N handles of the figure to those values which are in the input list of labels and those all were (what appears to my eyes to be) blue.
If that supposition is correct (and I'm pretty sure it is), you need to save and pass the corresponding line handles of the specific lines to be labelled as well as just the filenames.
Here, numel(file_list_1) is 12. for this case ,if you look the picture there is 12 bunch of curves (6 peak bunch and 6 bottom bunch).
matquestion.png
Yeah, but inside each of those there are at least three separate lines and so the twelve labels aren't actually being associated with the lines with the specific colors...in other words, the 12 lines you're actually labelling aren't the 12 that you need to label.
You'll need to save the handles of the actual lines drawn with the desired marker to call legend() with that array of line handles to associate the name.

Accedi per commentare.

Risposte (0)

Richiesto:

il 22 Lug 2019

Commentato:

dpb
il 22 Lug 2019

Community Treasure Hunt

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

Start Hunting!

Translated by