How to plot two curfeFit plots into one figure?

Hello people,
I have two scripts generated by the curveFit app that I want to have displayed in the same figure. They are linear regressions for two near constant temperatures from the same continuous measurement. What I am trying right now is the following:
%the lowtemp/hightemp vectors are snippets from t_Al and T_Al.
t_Al_lowtemp = t_Al(1:47);
T_Al_lowtemp = T_Al(1:47);
t_Al_hightemp = t_Al(160:end);
T_Al_hightemp = T_Al(160:end);
createFit_hightemp(t_Al_hightemp, T_Al_hightemp); %first fit for the higher temperature
hold on
createFit_lowtemp(t_Al_lowtemp, T_Al_lowtemp); %second fit for the lower temperature
plot(t_Al,T_Al); %the full continuous measurement
hold of
The fits themselves look good but all plots are in seperate windows. I have not touched the scripts generated from curveFit and since they are all the same, I have left them out.
How would I go about getting them all in one figure?
Thank you in advance!

 Accepted Answer

EmirBeg
EmirBeg on 9 Jun 2021
Edited: EmirBeg on 9 Jun 2021
If you look at the Curve Fitting script you generated, you can see that both scripts try to plot the results at the end. Looks like this in the CurveFitting functions.
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
legend( h, 'T_Al_hightemp vs. t_Al_hightemp', 'untitled fit 1', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 't_Al_hightemp', 'Interpreter', 'none' );
ylabel( 'T_Al_hightemp', 'Interpreter', 'none' );
grid on
That's why your get multiple plots in seperate windows.
You can just erase these and plot it in your own code with tiledlayout and hold on / holf off.

3 Comments

this might be a stupid question, but what parameters would I put into the plot when I make a plot outside the generated script? I am not at my computer right now so I can't play around with it and I have run into exactly this issue before but worked around it very unelegantly. Call the function without the plot command and then?
edit because I've read over this the first time: I do not want a tiledlayout. I want it all to be in the same figure, so overlaid with the graph from (t_Al,T_Al).
I just realized it would be even easier if you just erase the first line of the plot command in the function:
figure( 'Name', 'untitled fit 1' ); %erase this
This creates a new figure if you call the function but you don't want two figures.
Now you can plot both fits in one plot like this:
AL_H = createFit_hightemp(t_Al_hightemp, T_Al_hightemp);
hold on;
AL_L = createFit_lowtemp(t_Al_lowtemp, T_Al_lowtemp);
hold off;
You don't need to use plot() in your code now and it should work now.
Of course you can also modify the legend and the names of the axis so it all fits together.
Thank you, that did the trick. I did not realize that the figure command forced a new window! Now off to drawing pretty graphs.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products

Release

R2020b

Community Treasure Hunt

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

Start Hunting!