How can i plot each line with different colors
Show older comments
Hi,
i wrote a gui that can read and plot different variables from different sensors.
Here you can see the callback function of my 'Plot'-Button
function PlotButton_Callback(hObject, eventdata, handles)
% hObject handle to PlotButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
hold on;
PathName=evalin('base','PathName');
FileName=evalin('base','FileName');
SelectedELU=evalin('base','SelectedELU');
SelectedVar=evalin('base','SelectedVar');
FileName=sort(FileName);
PlotData=[];
TimeData=[];
d='1970-01-01 02:00:00.000'; %define start date of Timecode
t = datetime(d,'InputFormat','yyyy-MM-dd HH:mm:ss.SSS');
C = {'k','b','r','g','y',[.5 .6 .7],[.8 .2 .6]}; % Cell array of colros.
for r=1:length(SelectedELU)
for k=1:length(FileName)
load([PathName FileName{k}]);
y=eval(SelectedELU{r});
i=SelectedVar{1};
yy=y.(i);
PlotData=[PlotData yy];
yy=y.SYSTEMTIME;
yy= seconds(yy);
yy=yy(:)/1000;
yy= yy + t;
TimeData=[TimeData; yy];
end
% assignin('base','PlotData',PlotData)
% assignin('base','TimeData',TimeData)
plot(TimeData,PlotData','color',C{r},'DisplayName',SelectedELU{r});
end
% assignin('base','PlotData',PlotData)
% assignin('base','TimeData',TimeData)
ylabel(i);
axis 'tight'
The nested for-loop collects all the data from one sensor and the outer for-loop plots the data and starts the whole pocedure again for the next sensor.
The Problem is: All plots have the same color
i tried debugging with breakpoints and what i saw was: the first line is plotted in black as it should be, but when the second line is plotted the first one changes its color to blue, and when the third one is plotted the first two change their colors to red.
So everytime a line is plotted all existing lines change to the same color.
i'm pretty sure this can be fixed very easily but i just can't figure out how.
Thank You!!
Accepted Answer
More Answers (1)
KALYAN ACHARJYA
on 31 Oct 2019
Edited: KALYAN ACHARJYA
on 31 Oct 2019
Try
plot(TimeData,PlotData','color',C{randi(1,5)},'DisplayName',SelectedELU{r});
Another?
Have tou tried with hold on and plot in the same figure?
More:
Please ensure that C cell element must be change upto 5, so thet it catch the different color element in the cell array of C, as defined earlier.
I can't test the complete code without of complete input data.
1 Comment
Sebastian Körner
on 31 Oct 2019
Categories
Find more on Graphics Object Properties 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!