How to plot two curves (created from curve fitting toolbox) on the same graph?

80 次查看(过去 30 天)
I have two sets of X,Y data. I am creating a smoothing spline curve for each dataset, but this is as far as I can get. My objective is to export the curves for each data sets created from the curve fitter tool, then combine them on the same plot. Please see attached image of the first data set and curve obtained. This is the code I acquire after exporting for attached image:
%% Fit: 'untitled fit 1'.
[xData, yData] = prepareCurveData( X, Y );
% Set up fittype and options.
ft = fittype( 'smoothingspline' );
opts = fitoptions( 'Method', 'SmoothingSpline' );
opts.SmoothingParam = 0.99999;
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
legend( h, 'Y vs. X', 'untitled fit 1', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'X', 'Interpreter', 'none' );
ylabel( 'Y', 'Interpreter', 'none' );
grid off
Once i have a similar code for the second, how do I combine them on the same plot without the points (just curve)?
I am new to matlab so please go into as much detail as possible.
Thank you in advance.
  2 个评论
Abderrahim. B
Abderrahim. B 2022-8-31
编辑:Abderrahim. B 2022-8-31
Hi!
What do you mean by this My objective is to export the curves for each data set ? I am not sure, but I understood that you have multiple datasets and you want to fit them then plot all the datasets with the curves in the same figure !
Daniel Jones
Daniel Jones 2022-8-31
Hello. Yes, that is essentially what I am trying to do. I want the data points hidden however, with only the curves.

请先登录,再进行评论。

回答(2 个)

Matt J
Matt J 2022-8-31
编辑:Matt J 2022-8-31
You don't have to export code. You could just export the final fit objects fit1 and fit2, using "Export to Workspace",
whereupon you could do,
plot(fit1); hold on
plot(fit2); hold off

Abderrahim. B
Abderrahim. B 2022-8-31
Hi!
What I suggest is that you convert the code generated using the app to a function, modify it then use it within a for loop. Check the below:
% I don't have you dataset. Genearating some random datasets. Each column
% is a dataset
clear
Y = randi(5, 20, 5) ;
X = (1:20).';
numDataset = min(size(Y)) ;
col = jet(numDataset) ;
for ii = 1:numDataset
[resultFit, xdata, ydata ] = fitFnc(X, Y(:,ii)) ;
pH = plot(resultFit);
pH.Color = col(ii,:) ;
pH.LineWidth = pH.LineWidth + 0.5 ;
LegendArr{ii} = (strcat("CurveDataset", num2str(ii))) ;
hold on
end
hold off
legend(LegendArr, 'Location', 'southoutside')
function [fitResult, xData, yData] = fitFnc(X,Y)
%% Fit: 'untitled fit 1'.
[xData, yData] = prepareCurveData( X, Y );
% Set up fittype and options.
ft = fittype( 'smoothingspline' );
opts = fitoptions( 'Method', 'SmoothingSpline' );
opts.SmoothingParam = 0.99999;
% Fit model to data.
[fitResult, ~] = fit( xData, yData, ft, opts );
end
Hope you find this helpful.

类别

Help CenterFile Exchange 中查找有关 Smoothing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by