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

49 ビュー (過去 30 日間)
Daniel Jones
Daniel Jones 2022 年 8 月 31 日
編集済み: Matt J 2022 年 8 月 31 日
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 Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by