Is there a way to make matlab create variables of any number on its own?

조회 수: 13 (최근 30일)
I was making a programm that has some numbers that each time change because every time are generated random and I don't know the exact number of the variables I have to set due to this fact, so I was wondering if there is any command or function in matlab that helps me to create variables of any number, depending on the numbers that I get each time from an other variable. For example if a=10, I want matlab to create 10 new different variables, but the next time that I will run that programm and a=25, I want to be created 25 variables automatic without having to interfere myself.

채택된 답변

John D'Errico
John D'Errico 2015년 9월 7일
DON'T DO IT. This is just bad programming style, even if you do figure out a way to do it. (Yes there are ways to do so. They are not efficient. They are great ways to create buggy code.)
Learn to use arrays. The fact is, it is nearly as simple to type a(1) or a{1} as it is to type a1. Your code will be far better for it.

추가 답변 (3개)

the cyclist
the cyclist 2015년 9월 7일
One thing that might be useful for you is a cell array:
a = 10;
for na = 1:a
avar{na} = rand(na)
end

Hamoon
Hamoon 2015년 9월 7일
you don't need to define 10 variables, just define one and use its dimensions as different variables. If new variables are in the same "size" and "type" you can easily use an array with 10 rows and suitable columns. But if your new variables are arrays or strings in different sizes and types you can use cells:
b=cell(a,1);
and then use each cell as one variable.
b{1}='Hello';
b{2}=[1 2 3];
b{3}=[1 2 3; 4 5 6];
...
b{a}= 'anything';

Walter Roberson
Walter Roberson 2015년 9월 7일

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by