Saving function variables to workspace.

3 次查看(过去 30 天)
hello, I have the following code
function [fitresult, gof] = createFit(time, load)
%CREATEFIT(TIME,LOAD)
% Create a fit.
%
% Data for 'untitled fit 1' fit:
% X Input : time
% Y Output: load
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 08-Mar-2017 04:54:40
[Data] = xlsread('xyz.xls');
time1=Data(:,1);
load1=(Data(:,5));
[M,I] = max(load1(:));
tR=time1(I)+0.01;
k=0.0025;
R=2.5e-06;
x0=[2e6 4e6 1e7];
% load=(Data(:,5))*0.00980665;
%%Fit: .
[xData, yData] = prepareCurveData( time1, load1 );
% Set up fittype and options.
ft = fittype( '(((8*sqrt(R)*((k)^(3/2))*(sqrt(tR))*E1)./(2*((E1+E2)^2))).*((E2*(E1+E2).*tR)-(((E1.*eta*(exp(-(E1+E2).*time./eta)))*((-exp((E1+E2)*tR/eta))+1)))));', 'independent', 'time', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Algorithm = 'Trust-Region';
opts.Display = 'Off';
opts.Lower = [-1e2 -1e2 k -1e2 R tR];
opts.MaxFunEvals = 5000;
opts.MaxIter = 5000;
opts.StartPoint = [x0(1) x0(2) k x0(3) R tR];
opts.TolFun = 1e-12;
opts.TolX = 1e-12;
opts.Upper = [1e11 1e11 k 1e11 R tR];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'Best Fit' );
h = plot(fitresult,'-r', xData, yData,'-b' );
legend( h, 'load vs. time', 'Best Fit', 'Location', 'NorthEast' );
% Label axes
xlabel Time
ylabel Load
grid on
end
How can I save the results in the workspace to write them to a excel file using xlswrite, The variables don't show up in workspace. Making them global isn't working. Generated this code using curve fit toolbox. Thanks in advance.

回答(2 个)

Guillaume
Guillaume 2017-3-8
Assuming that "the results" is the fitresult outputput, simply call your function by giving it an output (the same way you get results from standard matlab functions, by the way)
>>fitresult = createFit(something, somethingelse);
fitresult will then be in the workspace of the caller, so if you call it from the command line, it'll be in the base workspace.
If by "the result" you mean some other variables such as xData, then you need to change the function prototype to tell it to return the value. E.g. for xData, change the function to:
function [fitresult, gof, xData] = createFit(time, load)
You would then call it with:
[fitresult, ~, xData] = createFit(something, somethingelse); %~ to ignore second output
  2 个评论
Shaunak Chandwadkar
Thanks Guillaume for the help, I gave it a try but, it did not work fore me. I guess its because I am trying to access
fitresult.E1
Which is in CFIT format.
Guillaume
Guillaume 2017-3-8
The first output returned by createFit will always be an object of class cfit that matches your fit model. So the property E1 should always be defined.
So if it does not work, what is the exact error message and what does
class(fitresult)
return?

请先登录,再进行评论。


Mohan Gopal Tripathi
Try using Debugger, when the step passes your "[fitresult, gof] = fit( xData, yData, ft, opts );", at that time it will create a variable gof with all information. But due to some reason it will not remain in workspace for ever. Either you create a variable and save it at that point, or just stop the step and see the details.
I also went through the same problem, and this is the way to get details of gof.
  1 个评论
Stephen23
Stephen23 2019-5-25
编辑:Stephen23 2019-5-25
"Either you create a variable and save it at that point, or just stop the step and see the details."
Those are fine if you a) want to save the variable OR b) are debugging your code. Otherwise you can just use the simple solution that Guillaume gave two years ago: specify that variable as an output argument AND then call the function with the appropriate number of outputs.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Fit Postprocessing 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by