필터 지우기
필터 지우기

i have variables 'Ereal2',E​real3,Erea​l4,Ereal5.​....so on.....how to call these variable in a single loop one by one.

조회 수: 1 (최근 30일)
These variables are vector in form
  댓글 수: 3
Stephen23
Stephen23 2018년 8월 1일
@RAVI YADAV: the most important question is: how did you get these variables into the workspace?. Most likely you did not sit a write them all out by hand, so they were likely imported using load. In which case you can trivially avoid this ugly situation by load-ing into an output variable:
S = load(...)

댓글을 달려면 로그인하십시오.

답변 (1개)

Dimitris Kalogiros
Dimitris Kalogiros 2018년 8월 1일
Hi Ravi
I have to agree with Jonas. But if you insist to use indexing which is embedded to the variable name, you can take some ideas from the following piece of code
clear; clc;
for n=1:10
% assign a value to each variable
strcmd=['Ereal' num2str(n) '=n^2 ;'];
eval(strcmd);
% display the value of each variable
disp(['Ereal' num2str(n) ' = ' num2str(eval(['Ereal' num2str(n)]))]);
end
The "key idea" is eval() function. You can find more info about this function on Mathworks help files
  댓글 수: 1
Stephen23
Stephen23 2018년 8월 1일
편집: Stephen23 2018년 8월 1일
" You can find more info about this function on Mathworks help files"
Indeed it is worth reading the documentation. For example, it states clearly that this method should be avoided "A frequent use of the eval function is to create sets of variables such as A1, A2, ..., An, but this approach does not use the array processing power of MATLAB and is not recommended. The preferred method is to store related data in a single array."
Dynamically accessing variable names is one way that beginners force themselves into writing slow, complex buggy code. Or you could just use indexing, which is neat, simple, easy to debug, and very efficient.
"But if you insist to use indexing which is embedded to the variable name..."
Then you force yourself into writing slow, complex, buggy, hard-to-debug code:

댓글을 달려면 로그인하십시오.

카테고리

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