이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
How can i use two variables while using eval command
조회 수: 5 (최근 30일)
이전 댓글 표시
Chaudhary P Patel
2022년 6월 4일
Sir/ Madam
I wrote this command for running for 100 time steps under for each n=5; when i wrote it like this
eval(['FSS',num2str(n),'=k_t*Uy(:,1)']);
and the result is coming like FSS1 as a string n.
but time step is not comming i want to involve time step also.
Please help me.
thanks.
댓글 수: 22
Stephen23
2022년 6월 4일
Your approach forces you into writing slow, complex, inefficient code that is buggy and hard to debug (as you are finding out now, where even your "simple" code does not work and you have to ask random strangers for help):
The simple and efficient approch uses indexing. Please explain the specific reason why you cannot use indexing.
Jan
2022년 6월 4일
Follow Stephen's hint. How and why to avoid EVAL was discussed hundrets of times in the forum. The result was the same in all cases: Other methods are better.
In addition your question is not clear: "but time step is not comming" - what does this mean?
Chaudhary P Patel
2022년 6월 4일
for i=1:1:10
for n=1:1:5
FSS(:,i+1)=k_t*Uy;
eval(['FSS',num2str(n),'=k_t*Uy(:,1)']);
end
end
When i am writing it, it is saving as FSS_n not FSS_i_n.
i want the result should save as FSS_i_n means i want i and n both syntax,
how can i wrote it?
Chaudhary P Patel
2022년 6월 4일
편집: Image Analyst
2022년 6월 4일
for i=1:1:10
for n=1:1:5
if n==1
Utt(:,i+1)=([zeros(nodof,1); (utdelt(1:nodof,i+1))]);
else
d1=1+(n-2)*nodof;
d2=6+(n-2)*nodof;
Utt(:,i+1)=utdelt(d1:d2,i+1);
end
end
% i want to save the Utt(6X1),for each i in different arrays as i shown in
% attached figure.
%utdelt(15X11), nodof=3,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1021580/image.jpeg)
Stephen23
2022년 6월 4일
@Chaudhary P Patel: if you show the information that I asked for, then someone can help you further.
Stephen23
2022년 6월 4일
@Chaudhary P Patel: how do you imagine processing all of those magically-named variables?
Image Analyst
2022년 6월 4일
What is "imagine processing"? Do you mean "image processing"? If so where is the image and what do you want to learn or measure about it?
Steven Lord
2022년 6월 4일
@Image Analyst I interpret @Stephen23's statement as "How do you imagine you are going to process all thoes magically-named variables?", or what's the poster's plan for working with those variables.
Image Analyst
2022년 6월 4일
편집: Image Analyst
2022년 6월 4일
@Steven Lord oh, right. I incorrectly thought it was the original poster asking a follow up question.
Jan
2022년 6월 4일
Did I mention already, that eval does not only impede the efficient execution of the code but also the clarity of the discussion?
Stephen23
2022년 6월 5일
"...not only impede the efficient execution of the code but also the clarity of the discussion?"
Indeed: https://xyproblem.info/
Chaudhary P Patel
2022년 6월 5일
i am not looking for any image analysis here. I shared that figure for example that i want result of Utt1, Utt2, Utt3, Utt4, Utt5 different tables of size 5X10 .
Sir, please suggest me how can i get it.
Jan
2022년 6월 5일
You got several suggestions for the solution already and exhaustive explanations. Simply ignoring them and asking again for the worst way of programming is not smart.
No, do not create a bunch of variables as Utt1, Utt2, ... but use any array: Utt{1}, Utt{2}, ...
I've posted some code which does this 18 hours ago already.
Chaudhary P Patel
2022년 6월 5일
@Jan sir, the code which you have send me, i checked but whatever i am not geting what i need. I am unable to explain you properly.
Sir, for further use of Utt1,.....Utt5 and for the previous value (i-1) of Utt1.......Utt5. It is requred to save it.
Stephen23
2022년 6월 5일
편집: Stephen23
2022년 6월 5일
"Sir, for further use of Utt1,.....Utt5 and for the previous value (i-1) of Utt1.......Utt5. It is requred to save it."
I very much doubt that.
If saving to a MAT file it would be simpler and more efficient to use the fields of a structure, thus giving exactly the same MAT file as if you had magically named variables (although even in that case, it is unlikely to be good data design).
If saving to some other file format, then the variable names are completely irrelevant.
So once again, you are presuming the (slow, complex, inefficient) solution and avoiding telling us what your actual task is:
"I am unable to explain you properly."
Stop telling us about your presumed solution.
Tell us what data you have (how it is created, what sizes, types, etc)
Tell us what you need to do with this data, giving the specific requirements. NOT your assumptions.
Chaudhary P Patel
2022년 6월 5일
%Sir this is code where i am stucking
for i=1:1:10
for n=1:1:nf %nf=5
if n==1
Utt(:,i+1)=([zeros(nodof,1); (utdelt(1:nodof,i+1))]);
else
d1=1+(n-2)*nodof;
d2=6+(n-2)*nodof;
Utt(:,i+1)=utdelt(d1:d2,i+1);
end
Utt(:,i+1)=Utt(:,i+1);
knt=Ktts;
f_s(:,i+1)=knt*Utt(:,i+1);
Uy= repmat(u_y, 2, 1); %u_y=[0.023;0.023;0.023]
f_r(:,1)=knt*Uy;
Flag(n)=any(abs(f_s(:,i+1))>abs(f_r(:,1)));
if Flag(n)
Utt(:,i+1)=Utt(:,i); % Here i am facing the problem for calling the i value. while i am calling it only taking Utt5 value.
f_S(:,i+1)=f_s(:,i);
else
Utt(:,i+1)=Utt(:,i+1);
f_S(:,i+1)=f_s(:,i+1);
end
end
end
Jan
2022년 6월 5일
@Chaudhary P Patel: I've showed you already how to simplify your code and how to store the vectors in different fields of a cell array. You insist on not using these working suggestions but keep your not working method.
"while i am calling it only taking Utt5 value" - There is no variable called Utt5.
Omit nonsense code like "Utt(:,i+1)=Utt(:,i+1);", because this increases the level of confusion only.
Then I cannot help you, unfortunately. Why do you waste your time with asking in the forum, if you are not interested in solutions?
I recommend to restart from scratch. Delete the complete code. Write down the problem mathematically with pencil and paper. Then implement it in code using the already suggested cell arrays.
Reduce clutter in the code to keep it as simple as possible:
for n=1:1:nf %nf=5
if n==1
Utt(:,i+1)=([zeros(nodof,1); (utdelt(1:nodof,i+1))]);
...
% Nicer:
Utt(:,i+1) = [zeros(nodof,1); utdelt(1:nodof, i+1)];
for n = 2:nf
...
Walter Roberson
2022년 6월 5일
d1=1+(n-2)*nodof;
d2=6+(n-2)*nodof;
Utt(:,i+1)=utdelt(d1:d2,i+1);
It would be clearer if you were to reshape utdelt to be 6 by nodof by something and then index utdelt(:, n-1, i+1) with no d1 d2 variables needed
Walter Roberson
2022년 6월 5일
Flag(n)=
are you going to use Flag after the loop? If not then do not bother to index Flag
else
Utt(:,i+1)=Utt(:,i+1);
f_S(:,i+1)=f_s(:,i+1);
sequences like that which copy data to itself just confuse the coding. If data is already stored where it belongs then just leave it there instead of copying it to itself
Stephen23
2022년 6월 6일
"Here i am facing the problem for calling the i value. while i am calling it only taking Utt5 value."
There is no variable named Utt5. But assuming that you actually mean that your code ignores all of the n iterations and only keeps the last one, then yes, because that is what you wrote your code to do. Your code (apparently, from my quick review) make no attempt to store the results of the n iterations, thus only the last one will appear in the workspace. You told MATLAB to keep overwriting the results of the n iterations, so that is what it does.
The solution to that is to use indexing (your approach using dynamic variable names would not actually help you, just make your code even more complex, and should be avoided). Reducing code clutter, as Jan suggested, would help too.
Either this is a mistake or a misleading way of distinguishing variables:
f_S(:,i+1)=f_s(:,i+1);
% ^ ^
답변 (1개)
Jan
2022년 6월 4일
If you really need different arrays, because the contents have different sizes, use a cell array:
Utt = cell(10, 5);
for i = 1:10
Utt{i, 1} = [zeros(nodof,1); utdelt(1:nodof, i+1)];
for n = 2:5
d1 = 1 + (n-2) * nodof;
d2 = 6 + (n-2) * nodof;
Utt{i, n} = utdelt(d1:d2, i+1);
end
end
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
아시아 태평양
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)