Plot the same color for all iterations of a loop

22 views (last 30 days)
I am importing *.mat files from a specified folder (I'm leaving that code out because it works fine). I then import the data into a 1x4 cell (dataT) and pull the file names out to use in my plot legend in the code below:
filePattern = fullfile(myFolder, '*.mat');
theFiles = dir(filePattern);
nFiles = numel(theFiles);
Out = cell(nFiles, 1);
dataT = cell(1, nFiles);
for k = 1:nFiles
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
[~, name] = fileparts(fullFileName);
dataT{k} = load(fullFileName);
Out{k} = name;
end
O=1;
runs=dataT{1,O}.runs;
Legend=cell(length(Out),1);
for p=1:length(Out)
Legend{p}=(Out(p,1));
end
Legend=string(Legend);
Then I process the data and plot 6 graphs. My objective is to plot the data from file 1 as one line on each graph and file 2 as another etc. with each plot, I want the color for the file to stay the same so it makes sense with my legend. This works well for my first 3 plots where I am calling x and y from dataT for a specified range of x. The next set of plots I need to do the same thing but for 20 iterations of the data and plot all 20 lines on each the graph. I am using a for loop to do this and the output has a different color for each iteration of the loop. I need all 20 iterations to be the same color AND to match the color of the file from the first set of plots. My plot command is inside a loop so calling out a color changes all 4 files to the same color.
The top is and example of the first set of plots. Let's say file 1 is red in the top plot, I need all 20 lines belonging to file 1 to be red in the bottom plot as well. Is there a way to do this? Thanks

Answers (1)

Cris LaPierre
Cris LaPierre on 30 Sep 2022
I don't see any code for actually creating the plots, so it's hard to understand exactly what you are doing.
If you do not specify color, then MATLAB changes the color for each line plotted based on the colors in colororder. By default, there are 7 colors. That means if you plot 8 lines, the first and 8th lines will have the same color.
Your options, then, are to
  1. specify the colors manually in your plot command
  2. specify the colororder to align with your plotting order
  1 Comment
Grace Hackenberg
Grace Hackenberg on 30 Sep 2022
Sorry I realize it was unclear now. I ended up creating a string array with all the color codes and then using a while statement instead of a nested for loop to run through all my data sets. I'm sure it's not the "correct" way to do it but it works for my purpose! Thanks for your response
color={'r','g','b','c','m','y','k','w'}
str=string(color)
col=2
while O<length(data)
O=O+1;
col=col+1
(processing a bunch of data here)
.
.
subplot(2,3,4)
title('title')
xlabel('xlab')
ylabel('ylab')
yticks([-600 -400 -200 0 200 400 600])
xticks([-15 -10 -5 0 5 10 15])
hold on
for f=1:m
plot(runs(f).Data(1).signals(4).Value, runs(f).Data(1).signals(2).Value,str(:,col))
end
hold off

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!