How to define variables automatically
17 views (last 30 days)
Show older comments
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 Comments
John D'Errico
on 21 Oct 2019
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
on 21 Oct 2019
Read this
and then just use indexing.
(indexing is simple, neat, and very efficient, unlike what you are trying to do).
Answers (2)
Ted Shultz
on 21 Oct 2019
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 Comments
Steven Lord
on 21 Oct 2019
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
on 21 Oct 2019
Image Analyst
on 21 Oct 2019
What does "define a variable automatically" mean to you? Do you mean
- 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
- 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.
0 Comments
See Also
Categories
Find more on Data Preprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!