現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
How can i use two variables while using eval command
3 ビュー (過去 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,
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 で Signal Processing Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)