Issues in legend color lines in for loop
1 view (last 30 days)
Show older comments
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);
3 Comments
dpb
on 22 Jul 2019
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.
Answers (0)
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!