How to define variables automatically

14 次查看(过去 30 天)
Hello I want to define some variables automatically say x1, x2, x3.... and want to assign inputs given by user to them automatically, can someone help?
  2 个评论
John D'Errico
John D'Errico 2019-10-21
Simple answer, don't do it. This is the path to poor, ungainly, inefficient, ugly code, a poor programming paradigm. It may stem from people using spreadsheets, where they want to continue with that style of programming.
Whenever you think you need to use those numbered variables, instead, learn to use vectors, arrays, matrices, cell arrays, structs, etc. Your code will be the better for it.
Stephen23
Stephen23 2019-10-21
Read this
and then just use indexing.
(indexing is simple, neat, and very efficient, unlike what you are trying to do).

请先登录,再进行评论。

回答(2 个)

Ted Shultz
Ted Shultz 2019-10-21
MATLAB will automatically resize vectors/matrix for you. You can put values into x(1:N) this way:
X(1) = rand()
X(2) = rand()
X(n) = rand()
If there are going to be large gaps in your index, you should use a sparse matrix.
  2 个评论
Steven Lord
Steven Lord 2019-10-21
That particular example is quite inefficient. Rather than calling rand N times to generate a scalar each time, call rand once to generate N elements.
X = rand(10, 1);
With this example, what Sachin Dighe referred to as x7 would instead be x(7).
Ted Shultz
Ted Shultz 2019-10-21
Yes, I was using rand() as a stand in for Sachin’s unknown variable assignment. Presumably Sachinis using some custom code, and not a built in MATLAB function to generate all these variables.

请先登录,再进行评论。


Image Analyst
Image Analyst 2019-10-21
What does "define a variable automatically" mean to you? Do you mean
  1. take the user's input and your script assigns that value "automatically" to a variable that has a name that you've given it when you wrote the script, OR
  2. Create variables in your script for which you don't know their name in advance.
I think case 1 is most likely since you said you want to "assign inputs given by user to them". So like you use inputdlg() to ask the user for x1, x2, and x3, and you then take the user's input and make the assignments. For this case, I give an example below where it's asking the user for an integer value and it assigns it to a variable in the script called "integerValue".
% Ask user for one integer number.
defaultValue = 45;
titleBar = 'Enter an integer value';
userPrompt = 'Enter the integer';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end % Bail out if they clicked Cancel.
% Round to nearest integer in case they entered a floating point number.
integerValue = round(str2double(cell2mat(caUserInput)));
% Check for a valid integer.
if isnan(integerValue)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
integerValue = defaultValue;
message = sprintf('I said it had to be an integer.\nTry replacing the user.\nI will use %d and continue.', integerValue);
uiwait(warndlg(message));
end
I decided, in advance (i.e. while writing the program, not while running it) upon the name "integerValue" and wrote it into the program and then I took the user's value and assigned that value to "integerValue".
The second case would require you to make variables with certain names, either gotten from the user (like inputdlg() asks the user BOTH for the variable name AND the value), or made by you according to some scheme (like an additional Xn for each number the user enters). Either one of these is a horrible idea, as others have told you.

类别

Help CenterFile Exchange 中查找有关 Data Preprocessing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by