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

0 Stimmen

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 26 Mär. 2020
Bearbeitet: Claudius Simon Appel am 26 Mär. 2020
Ok. That confuses me.
This is the solution/code I tried before posting this thread, as an extension of a suggestion that resolved the same problem with regard to the errorbars and data points:
plot(LPFits{1,k},'Color',colors(rem(k-1,3)+1,:))
That didn't work.
But your method
h = plot(LPFits{1,k});
h.Color = colors(rem(k-1,3)+1,:);
does work. I don't know why, I don't see the reason why the first approach wouldn't work, but thank you for solving this anyways.
Could you maybe explain what the difference is? From my perspective, there should be no reason for the first method to run itself into a wall, while the other succeeds.
Adam Danz
Adam Danz am 26 Mär. 2020
The difference is that LPFits{1,k} is a fitObject (I presume). Plotting fitObjects requires a different syntax than the typical plot() command.
See examples here
And info on the syntax here
Claudius Simon Appel
Claudius Simon Appel am 26 Mär. 2020
Hmm,okay. Thank you. Now I know what I will read through next. But at least now my project is finished, on to repeating the same thing for 3D plotting. Then we'll see how much of that I can just take over and what I have to read through again. I might upload the whole thing once it is sufficiently bug proofed, but I'm not sure on that, or if it would even be noticed^^.
Adam Danz
Adam Danz am 26 Mär. 2020
Sounds like you're on the right track. The time invested in reading through the documentation will pay off.
Claudius Simon Appel
Claudius Simon Appel am 26 Mär. 2020
Bearbeitet: Claudius Simon Appel am 26 Mär. 2020
Yea. That's basically what I have been doing the last two weeks, asking a problem here, being told what I needed for that, and reading through the documentation on that. Will meddle with the app designer next to find out how to improve something else. Thankfully, I set the whole thing up as 13 functions, so it's at least somewhat modular.
Claudius Simon Appel
Claudius Simon Appel am 27 Mär. 2020
Bearbeitet: Claudius Simon Appel am 27 Mär. 2020
I am sorry to open this again,
I integrated what you advised. I tried increase the color palette. I can use other colors instead of red/blue/green, and that works. However, when I define it with more than 3 colors, the function for some reason goes through the colors and plots the function in red --> blue --> green --> red --> blue --> green. The colors defined by colors are red --> blue --> green --> cyan --> magenta --> black --> yellow. As there are 6 graphs plotted, it should cycle all the way to black. But it doesn't and I don't see why.
At this point I don't want to open a follow up to a follow up, so I asking here in the comments directly.
Below is the code I am using for the plotting function. The same happens for the errorbar function.
function LPplotfun(LPmatx,LPmaty,LPFits,LPNumRows,LPErrorbarflag,LPplottypeindx) %LPplotfun plots all LPfit set up by LPfitfun
hold on
LPcolors = [1 0 0; % red
0 0 1; % blue
0 1 0; % green
0 1 1; % cyan
1 0 1; % magenta
0 0 0; % black
1 1 0]; % yellow
if LPplottypeindx==1 % Line plot
for k=1:1:LPNumRows
hold all
axis([-500 500 -500 500]) % this line controls the area in which the graphs are plotted initially, before
h = plot(LPFits{1,k}); %these two lines are necessary for the colours to match up
h.Color = LPcolors(rem(k-1,3)+1,:);
hold all % Set axis to higher values if you want to see more before adjusting the axes yourself.
if LPErrorbarflag==0
plot(LPmatx(k,:),LPmaty(k,:),'o','Color',LPcolors(rem(k-1,3)+1,:))
hold all
else
hold all
end % LPfits=LPfitfun(LPmatx,LPmaty,LPNumRows) %"LPfits" is the name of the cell in which all LPfits are stored.
hold on
end
end
Does anyone know why that happens/how to solve this?
Hint:
k = 1:6
rem(k-1,3)+1
% ans =
% 1 2 3 1 2 3
The '3' should be a variable, not hard-coded.
I can help further if needed :)
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 Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-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