How to make variable names using for loop
Show older comments
I need to make a code like below:
v1=0;
v2=0;
v3=0;
v4=0;
v5=0;
but how can i use for loop? i tried like below, which does not work.
for k=1:50
v{k}=0;
end
Answers (1)
Angelo Yeo
on 4 Oct 2022
0 votes
for k=1:50
v(k)=0;
end
10 Comments
Deokjae Jeong
on 4 Oct 2022
Deokjae Jeong
on 4 Oct 2022
Oh, okay. Please try the code below.
clear;
for k=1:50
eval(['v',num2str(k),'=0']);
end
Deokjae Jeong
on 4 Oct 2022
Edited: Deokjae Jeong
on 4 Oct 2022
Stephen23
on 4 Oct 2022
"I need to define 50 variables each by each. I cannot use matrix or vector for some reason. "
What is that "some reason" ?
Deokjae Jeong
on 4 Oct 2022
"How can I make inside this for loop vl1, vl2, vl3, vl4 .... etc?"
Ugh, do NOT do this: your bad data design forces you into writing bad code. The answer to your question is that you would have to write more slow, complex, inefficient, buggy code that uses EVAL for every single line of code.
"However, I encountered another problem below..."
The basic problem is actually that you are forcing meta-data (i.e. pseudo-indices) into the variable names. Instead of doing that, you should use arrays, just like MATLAB is designed for. Once you learn to use arrays, then your task will be simpler and much more efficient. Your current approach should be avoided.
" It is hard to explain because I am noob."
Then it is extremely unlikely that "I need to define 50 variables each by each. I cannot use matrix or vector for some reason", and this is much more likely:
Deokjae Jeong
on 4 Oct 2022
Stephen23
on 4 Oct 2022
"I dont have time right now because I need to finish it by tonight."
The inefficiency of your approach is not just runtime, but also programming and debugging time.
Walter Roberson
on 4 Oct 2022
You want to define and initialize 50 individual variables, and then you want to put the values of the variables together in a vector. You then overwrite the content of the individual variables with the results of the call.
In your perception, how does that differ from starting with just creating the vector (without putting the values into individual variables), passing the vector to the function, and then having code that splits the function up into the variables? Considering that you overwrite the variables after the call, what is going on in the code that requires that the initial values are written into individual variables before being put into the vector?
When you have code like
A=0; B=0;
C = [A, B];
then at the time the [A, B] is executed, the current values associated with A and B are extracted and put together into the list, and the resulting list is just of values. MATLAB does not keep any memory of where the values came from. If you were to examine C afterwards, you would be completely unable to determine that the first element was copied from A and the second from B. C=[A, B] is not creating a formula. If you were to alter A to 5 afterwards, C would not suddenly start evaluating to [5,0].
So inside the function that is receiving x0, there is no way to examine (for example) the 19th element of the vector and query "Which variable name did this element come from?"
An expression such as C=[A, B] is not creating a list of addresses of A and B. You cannot, for example, say "write 7 into the variable that was used for the second element of C" and then expect that B will now have the value 7. Only the values associated with the variables make it into C, with absolutely no "history" kept of where the values came from.
And that means that you might as well just use x0=zeros(1,50); without writing the values into individual variables first.
Categories
Find more on Loops and Conditional Statements 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!