How can i plot each line with different colors

2 vues (au cours des 30 derniers jours)
Sebastian Körner
Sebastian Körner le 31 Oct 2019
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!!

Réponse acceptée

Sebastian Körner
Sebastian Körner le 12 Nov 2019
function Plot_Callback(hObject, eventdata, handles)
% hObject handle to Plot (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
cla
hold all;
PathName=evalin('base','PathName');
FileName=evalin('base','FileName');
StringEPS=evalin('base','StringEPS');
SelectedELU=evalin('base','SelectedELU');
SelectedVar=evalin('base','SelectedVar');
FileName=sort(FileName);
d='1970-01-01 02:00:00.000'; %define start date of Timecode
t = datetime(d,'InputFormat','yyyy-MM-dd HH:mm:ss.SSS');
for r=1:length(SelectedELU)
PlotData=[];
TimeData=[];
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
try
Plot(:,r)=PlotData;
Time(:,r)=TimeData;
catch
if length(Plot)>length(PlotData)
a(length(Plot)-length(PlotData),1)=NaN;
b(length(Plot)-length(PlotData),1)=NaT;
PlotData=[PlotData; a];
TimeData=[TimeData; b];
Plot(:,r)=PlotData;
Time(:,r)=TimeData;
elseif length(Plot)<length(PlotData)
PlotData=PlotData(1:length(Plot),1);
TimeData=TimeData(1:length(Plot),1);
Plot(:,r)=PlotData;
Time(:,r)=TimeData;
end
end
end
for r=1:length(SelectedELU)
plot(Time(:,r),Plot(:,r),'DisplayName',StringEPS{r});
end
%assignin('base','PlotData',PlotData)
%assignin('base','TimeData',TimeData)
legend('Location','best')
ylabel(i);
xlabel('Date')
axis 'tight'
zoom on
dcm_obj = datacursormode(gcf);
set(dcm_obj,'DisplayStyle','datatip',...
'SnapToDataVertex','off','Enable','on', ...
'UpdateFcn',{@myupdatefcn})
i fixed the problem some time ago and wanted to complete this thread.
the solution to the problem was to put the plot command outside of the for loop. so i saved all my plot and time data in arrays and plot them in a seperate loop.
i dont know why it works like this and why it doesent work with the original version, but who cares as long as it works...

Plus de réponses (1)

KALYAN ACHARJYA
KALYAN ACHARJYA le 31 Oct 2019
Modifié(e) : KALYAN ACHARJYA le 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 commentaire
Sebastian Körner
Sebastian Körner le 31 Oct 2019
your code line doesn't work it produces an error.
but i changed my code to:
plot(TimeData,PlotData','color',C{mod(r,5)+1},'DisplayName',SelectedELU{r});
in order to avoid "index exceeds number of array elements"
yes i tried hold on and plot in the same figure, but i get the same outcome.
The problem ist not in plotting the lines in the wanted color, the problem is that all other lines change their color to the current one.
I'm very sorry but i can't give you data to test the code because this data is confidential.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by