How to control color inside a for loop

3 views (last 30 days)
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.
hold on
if LPErrorbarflag==0
hold on
hold on
end % LPfits=LPfitfun(LPmatx,LPmaty,LPNumRows) %"LPfits" is the name of the cell in which all LPfits are stored.
hold on
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 Comment
Geoff Hayes
Geoff Hayes on 26 Mar 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.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 26 Mar 2020
Edited: Adam Danz on 26 Mar 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,:);

Sign in to comment.

More Answers (0)


Find more on Line Plots 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!