Clear Filters
Clear Filters

Saving function variables to workspace.

3 views (last 30 days)
Shaunak Chandwadkar
Shaunak Chandwadkar on 8 Mar 2017
Edited: Stephen23 on 25 May 2019
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.

Answers (2)

Guillaume
Guillaume on 8 Mar 2017
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 Comments
Shaunak Chandwadkar
Shaunak Chandwadkar on 8 Mar 2017
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 on 8 Mar 2017
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?

Sign in to comment.


Mohan Gopal Tripathi
Mohan Gopal Tripathi on 24 May 2019
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 Comment
Stephen23
Stephen23 on 25 May 2019
Edited: Stephen23 on 25 May 2019
"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.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!