How can i plot each line with different colors
이전 댓글 표시
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!!
채택된 답변
추가 답변 (1개)
KALYAN ACHARJYA
2019년 10월 31일
편집: KALYAN ACHARJYA
2019년 10월 31일
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
Sebastian Körner
2019년 10월 31일
카테고리
도움말 센터 및 File Exchange에서 Axis Labels에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!