How do I make a function for running/potting "smoothing spline" on all my y plots? I can do it for one y plot, but can't figure out how to do the rest..thanks!

1 vue (au cours des 30 derniers jours)
How do run the code under "%Preparing /showing Curve data" on all my y plots?
if true
% code
end
delimiter = ',';
startRow = 2;
fmt=[repmat('%f',1,5) '%*[^\n]'];
pn = uigetdir(pwd,'matlab');
d=dir(fullfile(pn, '*.csv'));
L=length(d)
for i=1:L
msg='';
[fid,msg]=fopen(fullfile(pn,d(i).name),'r');
error(msg) % should never fail here w/ above check
dataArray=cell2mat(textscan(fid, fmt, ...
'Delimiter', delimiter, ...
'headerlines', startRow, 'collectoutput',1));
fid=fclose(fid); %closing the file
% if no data, abort, tell user
if isempty(dataArray),error(['No data from ' fullfile(pn,d(i).name)])
end
end
Time_Hrs=dataArray(:,1);
x1=Time_Hrs(1:end-1);
Control=dataArray(:,2);
dy1=diff(Control)./diff(Time_Hrs);
alphaCT1i=dataArray(:,3);
dy2=diff(alphaCT1i)./diff(Time_Hrs);
alphaCT1=dataArray(:,4);
ANT=dataArray(:,5);
dy3=diff(ANT)./diff(Time_Hrs);
ANT=dataArray(:,5);
dy4=diff(ANT)./diff(Time_Hrs);
%Plot Derivative
x2=x1(1:end-1);
plot(x1,dy1,x1,dy2,x1,dy3,x1,dy4);
%PREPARING /SHOWING CURVE DATA
[xData, yData1] = prepareCurveData( x1, dy1);
% Set up fittype and options.
% smooth curve applied ot a set of noisy observations
% my guess is that it marks the point where derivative is highest.
ft = fittype( 'smoothingspline' );
opts = fitoptions( 'Method', 'SmoothingSpline' );
opts.SmoothingParam = 0.787025547534865;
% Fit model to data.
[fitresult,~] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult);
legend( h, 'dy1 vs. x2', 'untitled fit 1', 'Location', 'NorthEast' );
% Label axes
xlabel Time(hrs)
ylabel 1stDerivative
grid on
deriv=h

Réponses (1)

Dimitris Iliou
Dimitris Iliou le 19 Mai 2017
If I understand correctly you want to get all the code that is below
%PREPARING/SHOWING CURVE DATA
and make it into a function in order for you to calculate all of your y plots.
You can easily turn that code into a function by doing the following:
function prepareData(x1,dy,FigureName)
%PREPARING /SHOWING CURVE DATA
[xData, yData1] = prepareCurveData( x1, dy);
% Set up fittype and options.
% smooth curve applied ot a set of noisy observations
% my guess is that it marks the point where derivative is highest.
ft = fittype( 'smoothingspline' );
opts = fitoptions( 'Method', 'SmoothingSpline' );
opts.SmoothingParam = 0.787025547534865;
% Fit model to data.
[fitresult,~] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', FigureName );
h = plot( fitresult);
legend( h, 'dy1 vs. x2', FigureName, 'Location', 'NorthEast' );
% Label axes
xlabel Time(hrs)
ylabel 1stDerivative
grid on
deriv=h
and you can call this function for each of your y's individually:
prepareData(x1,dy1,'Figure1')
prepareData(x1,dy2,'Figure2')
prepareData(x1,dy3,'Figure3')
prepareData(x1,dy4,'Figure4')
In case you want to plot everything in the same figure, you could use the figure handle to do it.

Catégories

En savoir plus sur Linear and Nonlinear Regression dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by