create a number of variables in workspace and assign value to them using input command
조회 수: 6 (최근 30일)
이전 댓글 표시
i want to write a script that first gets the number of inputs(e.g:n=4) and then creates the respective variables(e.g:var1,var2,var3,var4) in workspace so that they can be assigned values(integer or matrix) by user using the 'input' command so that they can be used in my equations.i've tried something like this but it doesn't even produce variables in workspace:
1.n=input('number of inputs=');
2.for i=1:n
3.fprintf('var%d=',i)
4.eval(['var',num2str(i)])=input('')
5.end
notice that i can't use the following script:
1.for i=1:n
2.fprintf('var%d=',i)
3.var(i)=input('')
4.end
because in this script it creates only one variable in workspace; a matrix named var, and the values will be stored in its cells(therefor the inputs can't be matrices.) also i'm interested to know how exactly i should use the 'eval' command and when.thank you.
댓글 수: 0
답변 (2개)
KSSV
2016년 10월 20일
It is not recommended to name the variables as you said. Refer the following link.
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!