trying to use a while loop to run through a list created by user inputs to plot one or multiple functions

Hi there, I'm trying to do a project in where I need a user input to choose from 4 predetirmined functions to plot. I don't have a lot of expirience in matlab, but if I was doing this in python, I would create a list from the user input, run a while loop, use .pop() to delete each of the inputs one by one and run them as a new variable, and then store each of the variables to a new list using append.
So far I have not had a lot of sucess in matlab.
As far as I can tell, there does not seem to be an appicable way to do what I was sugesting, and I was wondering if any one had any pointers.
prompt= 'please choose which funtions you want to plot. A, B, C, and/or D.';
a=[];
g=list(input(prompt));
while g
%plot_list=g.pop()
o(a) = o;
if g== 'A'
x = linspace(0,2.*pi,1000);
y = sin(2.*pi.*10.*x);
o=plot(x,y)
elseif g== 'B'
h = linspace(0,2.*pi,1000);
i = (exp(-x))*cos(5*x);
o=plot(h,i)
elseif g== 'C'
j = linspace(0,2.*pi,1000);
k = sinc*x;
o=plot(j,k)
elseif g== 'D'
l = linspace(0,2.*pi,1000);
m = (x.^3)+x;
o=plot(l,m)
end
end
o(a) = o;

1 commentaire

If you want a user input to plot respective graph then why would you need to delete and append something to your list?

Connectez-vous pour commenter.

 Réponse acceptée

prompt = 'please choose which funtions you want to plot. A, B, C, and/or D.';
g = (input(prompt,'s'));
figure(); % all lines will be plotted in one axes
hold on % don't let subsequent plots erase prior ones, which is the default behavior
a = []; % an array of graphics handles to all plotted lines, initially empty
while ~isempty(g)
% pop the first element off of character array g:
f = g(1); % first store the first character of g as f
g(1) = []; % then remove the first character from g
if f == 'A'
x = linspace(0,2.*pi,1000);
y = sin(2.*pi.*10.*x);
elseif f == 'B'
x = linspace(0,2.*pi,1000);
y = (exp(-x)).*cos(5*x);
elseif f == 'C'
x = linspace(0,2.*pi,1000);
y = sinc(x);
elseif f == 'D'
x = linspace(0,2.*pi,1000);
y = (x.^3)+x;
else
continue
end
a(end+1) = plot(x,y); % append the new plot to the end of a
end

Plus de réponses (1)

if I was doing this in python, I would create a list from the user input, run a while loop,
use .pop() to delete each of the inputs one by one and run them as a new variable, and
then store each of the variables to a new list using append.
The below code assumes homogenous data that can be indexed using () indexing. This is not strictly restricted to numeric values -- it can, for example, be used for string() objects and for logical values and character scalars, and also can be used for graphic objects. It cannot, however, be used for function handles or for keeping character vectors separate from each other. It is not difficult to modify the code to work with mixed datatypes .
function [current_item, input_list] = pop(input_list)
if isempty(input_list)
current_item = [];
else
current_item = input_list(1);
input_list = input_list(2:end);
end
end
function input_list = append(current_item, input_list)
if isempty(input_list)
input_list = current_item;
else
input_list(end+1) = current_item;
end
end
g=list(input(prompt));
Caution: MATLAB does not have any list() function. Most things in MATLAB are already arrays.
In MATLAB, the input() function with no options reads a line of input from the user, and executes the line and returns the value. If the user had, for example, entered C at the prompt, then input() used the way you used it, would attempt to execute C as code. If you happened to have a variable named C then execution would result in the content of the variable; if you did not happen to have a function or variable named C, then execution would give an error.
When that form of input() is used, the user can enter a list of values -- but the user has to know to use MATLAB list form. For example if the user enters
1 2
then because it is not in list form, then that would generate an error. The user would have to know to enter
[1 2]
Likewise, if you are expecting the user to enter a list of letters be considered individually, the user would have to know to use apostrophes, such as
'AB C'
The result of which would be a vector of four characters, that could be indexed -- so for example g(2) would hold 'B'
If the user entered
"AB C"
then because this would be executed as a MATLAB statement, the result would be a string() scalar that you cannot index with (2) because there is only one string.
There is a major option for input() that you should consider using: if you use the 's' option, such as
g = input(prompt, 's')
then MATLAB will read the line of input as a character vector and will return the character vector un-interpreted. As it is a character vector rather than a string() object, individual characters could be indexed.
a=[];
o(a) = a;
In MATLAB, on the first iteration, since o is not defined yet, that would attempt to create a variable named o and to index into o and to place the content of a at the location indexed. But a is empty because you initialized it as empty. So you are indexing into no location of o and assigning nothingness to there. That will, surprisingly, be accepted, and will cause o to be initialized to an empty vector of double precision objects. You never change a in your code, so each time you encounter that statement it will replace o with an empty double-precision array. There does not seem to be much point in that?
if g== 'A'
Caution: if g is character type and since the literal 'A' is character type (not string() object) then the == operator is a position-by-position comparison. Because 'A' is scalar, that is a special case and each position in g would be compared to the character 'A' and the result would be a logical vector the same size as g . That might not be what you wanted: you might have wanted to know whether the content of g as a whole matched 'A' rather than whether each position matched 'A' . To compare as a whole, use strcmp() . You also need strcmp() if the items to be compared are potentially different lengths, such as
g = 'hi';
if g == 'high'
which would be an error because you would be trying to compare a vector with 2 positions against a vector with 4 positions.

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by