필터 지우기
필터 지우기

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

조회 수: 65 (최근 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 CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by