MATLAB: How to use a GUI to set the polynomial level for a graph after examining the data

Curve Fitting ToolboxMATLABpolynomial curve fitting

Hi there – me again!
I'm trying to make my program more and more clever but keep falling down when not understanding how to enter variables into functions of Matlab that expect a certain order. My bit of code I've made nicely shows the graph I want to fit the curve to, but, when the user enters the polynomial level, I can't get fit() command to use it.
Any help appreciated! Should I be using a different command to fit()?
% Fit a polynomial to the data
prompt2={'Enter the level of polynomial to use):'};
% Create all text fields with the questions specified by variable prompt
box2title='Polynomial Selection';
% The main title of your input dialog interface.
answer2= inputdlg(prompt2,box2title);
[Width_fit, gof] = fit( v1,wdth, answer2, 'Normalize', 'on' );
It's exspecting things like 'poly5' rather than just 5.
Thanks
Vadim

Best Answer

Vadim - since the fit function is expecting poly5 then you will have to prepend poly to the result from the input dialog. If you assume that the user is just entering a numeric value, then try the following
answer2= inputdlg(prompt2,box2title);
% create the fitType
fitType = ['poly' char(answer2)];
% call the function
[Width_fit, gof] = fit( v1,wdth, fitType, 'Normalize', 'on' );
Note that the conversion from a cell array to a character array may be needed (that is why answer2 is wrapped in a call to char).
Try the above and see what happens!