How to plot two curves (created from curve fitting toolbox) on the same graph?
49 views (last 30 days)
Show older comments
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 Comments
Abderrahim. B
on 31 Aug 2022
Edited: Abderrahim. B
on 31 Aug 2022
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 !
Answers (2)
Abderrahim. B
on 31 Aug 2022
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.
0 Comments
See Also
Categories
Find more on Linear and Nonlinear Regression 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!