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

49 visualizzazioni (ultimi 30 giorni)
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 Commenti
Abderrahim. B
Abderrahim. B il 31 Ago 2022
Modificato: Abderrahim. B il 31 Ago 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 !
Daniel Jones
Daniel Jones il 31 Ago 2022
Hello. Yes, that is essentially what I am trying to do. I want the data points hidden however, with only the curves.

Accedi per commentare.

Risposte (2)

Matt J
Matt J il 31 Ago 2022
Modificato: Matt J il 31 Ago 2022
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 il 31 Ago 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.

Categorie

Scopri di più su Linear and Nonlinear Regression in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by