How to control color inside a for loop

조회 수: 5 (최근 30일)
Claudius Simon Appel
Claudius Simon Appel 2020년 3월 26일
댓글: Adam Danz 2020년 3월 27일
Hello, this is a follow up to this question.
I asked if it is possible to control the color used by graphs plotted by a for loop.
Ameer Hamza's answer made it possible to controll the color used by both the plotting of the data points (if no errorbars are plotted, as well as of the errorbars.
However, I can't figure out how to implement this for the plotting of the fits themselves. The fits are still using random colors, and not those specified. If I try to apply Hamza's solution to the plotted fits, I am greeted with
>> LPplotfun(LPmatx,LPmaty,LPFits,LPNumRows,LPErrorbarflag,LPplottypeindx)
Error using cfit/plot>parseinput (line 332)
Must specify both XDATA and YDATA.
Error in cfit/plot (line 46)
[S1,xdata,ydata,S2,outliers,S3,ptypes,conflev] = parseinput(alltypes,varargin);
Error in LPplotfun (line 11)
plot(LPFits{1,k},'Color',colors(rem(k-1,3)+1,:)) % the user adjusts the axes himself.
All of the above are error outputs from the command window.
This is the code for the plotting of the fits, but not the complete function.
function LPplotfun(LPmatx,LPmaty,LPFits,LPNumRows,LPErrorbarflag,LPplottypeindx)
colors = [1 0 0; % red
0 0 1; % blue
0 1 0;]; % green
if LPplottypeindx==1 % Line plot
for k=1:1:LPNumRows
hold on
axis([-500 500 -500 500]) % this line controls the area in which the graphs are plotted initially, before the user adjust them himself later on
plot(LPFits{1,k},'') % Set axis to higher values if you want to see more before adjusting the axes yourself.
%plot(LPFits{1,k},'Color',colors(rem(k-1,3)+1,:))
hold on
if LPErrorbarflag==0
plot(LPmatx(k,:),LPmaty(k,:),'o','Color',colors(rem(k-1,3)+1,:))
hold on
else
hold on
end % LPfits=LPfitfun(LPmatx,LPmaty,LPNumRows) %"LPfits" is the name of the cell in which all LPfits are stored.
hold on
end
end
end
This is not the entire code of the function, but all that is necessary. Technically the function can also plot bar plots and other ones, but this is not what I am concerned with right now. If needed, I can provide the full function. This is also why the variable "LPplottypeindx" is not used in this part. All that is missing is using the LPplottypeindx to control which part of the whole function is run. (In case of normal line plots it's the code shown above.)
The line "%plot(LPFits{1,k},'Color',colors(rem(k-1,3)+1,:))" is what I tried to get working, and what is causing the error messages shown above.
Thank you once again.
Note: Technically I am running Matlab R2020a now, but I checked and the same problem persists in R2019b. However, since I cannot yet choose R2020a as my used Release version, I chose R2019b.
  댓글 수: 1
Geoff Hayes
Geoff Hayes 2020년 3월 26일
Claudius - what is LPFits{1,k}? Please provide the dimensions, data type (is it a fitobject?), or a small subset of data from this.

댓글을 달려면 로그인하십시오.

채택된 답변

Adam Danz
Adam Danz 2020년 3월 26일
편집: Adam Danz 2020년 3월 26일
I haven't dug too deeply into the two threads but I think this will work for you. If not, I'll remove the answer so the question isn't marked as answered.
h = plot(LPFits{1,k});
h.Color = colors(rem(k-1,3)+1,:);
  댓글 수: 9
Claudius Simon Appel
Claudius Simon Appel 2020년 3월 27일
Perfect, that resolved it. Thank you.
For some reason, I misunderstood that line as something completely different. Thank you.
Adam Danz
Adam Danz 2020년 3월 27일
High five!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by