Adding a second scaled y-axis to a plot

Hello,
I have been trying to figure out how to add a second scaled y-axis ( or even just a second y-axis) and even after reading through several Matlab forum posts it still does not work. I was wondering if anyone out there, would help me figure out what I am doing wrong.
My personal thoughts on what is wrong is something to do with the fact that I am trying to plot a curve fit on a set of data and when I tell Matlab to plot according to some x-axis is can not compute my request.
My matlab code so far:
x=[
0
3
6
9
11.5
13.952
];
y=[
0.905
0.900
0.850
0.800
0.788
0
];
v=[
0
3
6
9
11.5
13.952
];
p=[
0
2.5
5.1
8.9
11.493
0
];
f=fit(v,p,'poly5')
c=fit(x,y,'exp2')
figure(1)
plot(f)
figure(2)
x= 0: 1.0 : 15;
plot(f);
hold on;
plot(c)
Where x,y are plots for one curve and v,p (x and y coordinates respectivly) for the other curve
I have tried,
x= 0: 1.0 : 15;
plot(x,f);
hold on;
plot(x,c)
and I tried to follow the example given in https://se.mathworks.com/matlabcentral/answers/23591-examples-of-multiple-plots-on-one-graph ( more specifically the accepted answer given by Chandra Kurniawan) , where a problem similar to what I am facing now was asked and answered.
Any help/thoughts/advice is extremely welcome! Thank you in advance

 采纳的回答

You can use yyaxis to plot 2 axis
x = 0:0.1:10;
yyaxis left
plot(x, x);
yyaxis right
plot(x, sin(x))

6 个评论

By looking at your example I have changed my code to the following,
f=fit(v,p,'poly5')
c=fit(x,y,'exp2');
x = 0:1:15;
yyaxis left
plot(x,c);
yyaxis right
plot(x, f)
I get an error for some reason and I am not entirely sure what. The following error message was produced,
Error using cfit/plot>parseinput (line 335)
Must specify both XDATA and YDATA.
Error in cfit/plot (line 42)
[S1,xdata,ydata,S2,outliers,S3,ptypes,conflev] = parseinput(alltypes,varargin);
Error in LEPLOTS1 (line 47)
plot(x,c);
The only thing I can think of that is conflicting with the matlab script is that fact that I ask it to fit a series of points and then use that fit which I have created in a plot.
If you want to plot cfit object then you cannot specify x. Use it like this
yyaxis left
plot(c);
yyaxis right
plot(f)
If you want to specify your own x then use
yyaxis left
plot(x, c(x));
yyaxis right
plot(x, f(x))
Seems to working, thanks everyone for the help! Only problem now is my cfits as they for some reason decide to take the form of relativly high negative values for no apparent reason. I should be able to figure that one out though.
Not sure whether it is the problem with the fitting parameters. But you might want to check the range of x being displayed on the axis.
IT WORKED!!! THANKS !!!! 6 hrs of lovely troubleshooting is finally done!!!!!!!! Thanks a lot!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息

产品

版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by