Script for curve fitting

Hi, being very newish to Matlab I am trying to solve a simple problem to begin with, and advance from there. I have 2 sets if data (named x and y in workspace). When I use curve fitting tool to fit the data, it works fine, but if I use the script below in .m file, I get error saying more input arguments are needed to run.
ft = fittype ('poly4'); [xdata, ydata] = prepareCurveData(x,y); c = fit(xdata,ydata,ft);
I have tried the fit from curve fitting toolbox and using generate code function and I get the same error. What am I missing here?
Any help is appreciated.

Answers (1)

'Generate code' usually returns a function and not a scrip. Is it possible that you are not calling the function with the right arguments?
Could you paste the exact error message from the command line?
Also, could you run the following and see if it works?
>> x = rand(10,1);
>> y = rand(10,1);
>> ft = fittype ('poly4'); [xdata, ydata] = prepareCurveData(x,y); c = fit(xdata,ydata,ft);

4 Comments

KV
KV on 19 Mar 2013
Edited: KV on 19 Mar 2013
Thanks Shashank I believe the issue arises from the function input arguments, as with random numbers it works fine.
This code from code generator gives same error:
function [fitresult, gof] = rubb(x, y)
%Fit: 'rubb'.
[xData, yData] = prepareCurveData( x, y );
% Set up fittype and options.
ft = fittype( 'poly4' );
opts = fitoptions( ft );
opts.Lower = [-Inf -Inf -Inf -Inf -Inf];
opts.Upper = [Inf Inf Inf Inf Inf];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
The error message given is "rubb" required more input arguments to run. Any suggestions on what arguments are missing, and what I need to add i order to make this script runnable?
Being a LabVIEW person, Matlab has given me a steep learning curve :/
Thanks
I created random numbers to show you an example. you can use your own data. I will use random numbers again.
>> x = rand(10,1);
>> y = rand(10,1);
>> [fitresult, gof] = rubb(x, y)
Does executing the above in the command line work for you?
I think the issue is that you are calling the funcion rubb with no arguments although it clearly has two arguments x and y, which you need to pass to it inorder for it to return the fit result.
Shashank Prasanna
Shashank Prasanna on 19 Mar 2013
Edited: Shashank Prasanna on 19 Mar 2013
Just to add to that, you can't just execute the generated code. You will need to call it as showed above, either from the command line or from another script. hth.
KV
KV on 20 Mar 2013
Edited: KV on 20 Mar 2013
Your commands execute perfectly fine in command window. I am confused as to what other possible input argument there can be that I am missing. I have just tried running and calling the m script file containing just the code below:
function [c,z] = createFit(x,y);
[a, b] = prepareCurveData(x,y);
ft = fittype ('poly4');
c = fit(a,b,ft);
z = c(750);
Now when I run this m script file, it executes fine but if I call it from another script (which is what I eventually want to do) or from command window then it gives same error again (Error using createFit (line 2) Not enough input arguments).
I am struggling to understand what am I missing.

Sign in to comment.

Categories

Asked:

KV
on 19 Mar 2013

Community Treasure Hunt

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

Start Hunting!