How to manipulate data provided by a user via a dialog box?

2 views (last 30 days)
Hi everyone --
I'm new to Matlab so excuse me if this is a simple question. I'm trying to write a program that allows a user to type in a single variable equation in a dialog box, a range for that equation, and an increment for that equation. The program then needs to generate a table and a plot of the equation.
I have my dialog box set up like this:
>> prompt = {'Enter your equation:','Enter minimum x:','Enter maximum x', 'Enter your increment:'};
>> name = 'Input for user''s function';
>> numLines = 1;
>> userInput = inputdlg(prompt,name,numLines);
As you can see I store the information in the cell userInput. How do I get the data out so that it's in a workable format? For example, I want to create a variable representing the whole range, like this:
>> range = userInput(2):userInput(3)
But Matlab gives me the error "Undefined function 'colon' for input arguments of type 'cell'."
I also want to plot the user's function like this:
>> plot(userInput(1))
But get the error "Error using plot. Conversions to double from cell is not possible."
So, how can I convert these user inputs into something I can work with? I am really stuck. Thank you for your help!

Accepted Answer

Matt Fig
Matt Fig on 22 Sep 2012
Edited: Matt Fig on 22 Sep 2012
With cell array you have to access the contents using curly braces. Also note that the data is a string, not a number.
range = str2double(userInput{2}):str2double(userInput{4}):str2double(userInput{3});
func = vectorize(inline(userInput{1}));
plot(range,func(range))

More Answers (1)

Casey
Casey on 22 Sep 2012
This did the trick, thank you!

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!