How can I plot a curve while changing color every N points ?

7 views (last 30 days)
Hello !
I am working on a code to plot my datas. Thoses datas are measurements of N cycles, each have 162 points. The thing is that my ouput is one .txt file of N*162 lines. I can plot those but I am struggling to find how I can associate a different color every 162 points (and in my dreams those colors are also a gradient so we can see the evolution clearly). I tried to do it as a for loop :
C=() %Here I should put something to sweep a gradient color range but I have nonn clue what can I put
L=linspace(-40,40,PpC/2); %PpC is an input which refers to the number of points per cycle, so 162
figure
for j=1:NdC % NdC is an input which refers to the number of cycles in the experiment, what I called N just before
plot(L,mapcomplete,'color',C(j))
end
Obvisouly this doesn't work but I just would like to give you the path I am working on.
Also I link an example of .txt file I use if it might help.
Thank you a lot !!
I.

Accepted Answer

Jon
Jon on 26 Jul 2022
Edited: Jon on 26 Jul 2022
I was coding up the example below, and by the time I posted I saw you already had an answer from @Cris LaPierre. I will still include mine in case it provides you with any further ideas. I had understood that you wanted a seperate curve for each cycle, each with a different color, rather than just one curve with the colors changing as @Cris LaPierre shows.
% make some example data
numCurves = 30;
x = linspace(0,100,50)';
Y = x*linspace(0,25,numCurves);
% make color map
colors = jet(numCurves); % could use others, e.g csv, winter etc
% plot curves
figure
hold on
for k = 1:numCurves
plot(x,Y(:,k),'Color',colors(k,:))
end

More Answers (1)

Cris LaPierre
Cris LaPierre on 26 Jul 2022
You would have to have a separate plot call for each line segment. Maybe something like this?
N = 10;
C=parula(N); % returns 256 colors from the parula colormap
L=linspace(-40,40,N*162);
Y=rand(size(L));
ind = 0:162:length(L); % indices of bounding each group
for j=2:length(ind)
plot(L(ind(j-1)+1:ind(j)),Y(ind(j-1)+1:ind(j)),"Color",C(j-1,:))
hold on
end
hold off

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!