I'm trying to get graphics with plot and inputdlg.However, the more I tried, the more I get confused :/
Also, I have to find answers of question:
And this is what I'd tried to write:
function Function2()
while 1
prompt={'Fonksiyonunuzu Giriniz:'};
name='Grafik Çizici';
func=inputdlg(prompt,name);
if isempty(func)==1
prompt={'Are you sure, press y or n:'};
a=inputdlg(prompt);
if 'y' ;
break;
end
else
plot(func)
end
end
end
Thanks in advance for helping and suggestions :)
First thing, from the documentation for inputdlg
, the output is a cell array, so you need to index it properly . eg if a{1} == 'y'
Second thing, if I'm interpreting the purpose of this program correctly you're going to need to decide what form you want your input functions to be. If you assume that the user will provide the input function string as an anonymous function , things are slightly easier. Otherwise you need to create a parser for your func
variable to properly set up the function to evaluate and plot. Having the symbolic also makes it pretty simple but I don't have it so I can't construct and test examples.
That being said I'm going to assume the input is an anonymous function:
function plotanonymous()
% Prompt for function to plot
funcprompt = 'Input Anonymous Function to Plot:';
funcprompttitle = 'This is a popup';
func_str = inputdlg(funcprompt, funcprompttitle); % My input will be @(x) x.^2
% Add input validation here
func_anon = str2func(func_str{1}); % Convert function from string to anonymous function
% Prompt for evaluation limits
limitprompt = {'Input Lower Limit'; 'Input Upper Limit'; 'Input Spacing Interval'};
limitprompttitle = 'This is a popup';
evallimits_str = inputdlg(limitprompt, limitprompttitle);
% Add input validation here
% Set up data to plot
evallimits_dbl = str2double(evallimits_str); % Convert to double
x = evallimits_dbl(1):evallimits_dbl(3):evallimits_dbl(2); % Set up x data
y = func_anon(x); % Evaluate input function for input x
% Plot data
% Create a handles structure so we can easily modify properties
h.myfig = figure;
h.myplot = plot(x,y);
end
Now we need to talk about input validation. Your example seems to have started to look at this but I don't think it's functioning like you'd want. As written, if the input is empty it asks if you're sure. If yes, you attempt to break out of the while loop, if no you go ahead and plot the function. I would use a try/catch loop with the str2func
call to give the user another chance to input a valid function. You could also place the user in a while
loop until a valid input function is provided.
Give this a go and come back with more questions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.