Replace "eval" in vectors created inside a loop

조회 수: 1 (최근 30일)
Alonso
Alonso 2016년 3월 8일
댓글: Star Strider 2016년 3월 14일
I created a GUI that requests info from the user, which is then saved into series of variables (e.g. Fp1, Sn1, Fp2, Sn2, Fp3, Sn3...) and into different files (Info1.mat, Info2.mat, Info3.mat...). Once I have collected all of them, there's a vector created inside a loop with the value of each one of them. For example:
Fp1=0.8;
Fp2=0.9;
Fp3=0.85;
and I want to get:
Fp=[0.8 0.9 0.85];
I have already tried the following code:
for jj=1:1:ng
Fp(jj)=eval(sprintf('Fp%d',jj),ii);
end
which is working fine, but I would like to know if there're other suggestions to do the same without "eval". Thank you in advance.

답변 (1개)

Star Strider
Star Strider 2016년 3월 8일
In your GUI, I would simply save them to ‘Fp(1)’ instead of ‘Fp1’ when they’re entered, and so for the others. Providing that they’re scalars (and they are in your example), that should work.
  댓글 수: 4
Alonso
Alonso 2016년 3월 14일
Oh I see, the thing is that the values can't be requested by dialog boxes, but they're entered in different edit boxes (Fp is the one in the red circle) from a GUI; so when all the information has been written and the user pushes "Siguiente" (Next), everything saves into a .mat file which will make some calculations later.
Star Strider
Star Strider 2016년 3월 14일
I have no idea how your code is importing them from your GUI. If it is importing them as a vector or cell array, you can address individual elements of the vector or cell array.
In any event, if you are now importing them as ‘Fp1’ now, you can likely change your code to import them instead as ‘Fp(1)’, and so for the others.

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

카테고리

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