How to control color inside a for loop

5 Ansichten (letzte 30 Tage)
Claudius Simon Appel
Claudius Simon Appel am 26 Mär. 2020
Kommentiert: Adam Danz am 27 Mär. 2020
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 Kommentar
Geoff Hayes
Geoff Hayes am 26 Mär. 2020
Claudius - what is LPFits{1,k}? Please provide the dimensions, data type (is it a fitobject?), or a small subset of data from this.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Adam Danz
Adam Danz am 26 Mär. 2020
Bearbeitet: Adam Danz am 26 Mär. 2020
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 Kommentare
Claudius Simon Appel
Claudius Simon Appel am 27 Mär. 2020
Perfect, that resolved it. Thank you.
For some reason, I misunderstood that line as something completely different. Thank you.
Adam Danz
Adam Danz am 27 Mär. 2020
High five!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by