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

2 views (last 30 days)
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

Accepted Answer

Geoff Hayes
Geoff Hayes on 24 Jan 2015
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!
  1 Comment
Geoff Hayes
Geoff Hayes on 26 Jan 2015
Vadim's answer moved here
Many thanks, Geoff,
I didn't understand the language to add 'poly' to a number. I see now using the char command that makes it possible and it worked no problem. Thanks very much for your input.
The next level of battle is trying to let the user select from a list, Poly, Poly3, Poly 4, Poly 5, Exp, power, etc etc and visualize it.A bit like building the curve fitting toolbox into my program!
I may be taking it too far but I kind of see how to do it. I just need a list of selectable inputs and the answer outputs including all the different options. I'd then have to hold the figure using a uiwait(msgbox) command until the user said "OK, I'm happy with that fit" otherwise keep running the loop re-plotting the graph with the new selection.
For loops, oh the joys!
Thanks again for your help.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!