You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How can i use two variables while using eval command
3 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 4 Jun 2022
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
on 4 Jun 2022
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
on 4 Jun 2022
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
on 4 Jun 2022
Edited: Image Analyst
on 4 Jun 2022
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
on 4 Jun 2022
@Chaudhary P Patel: if you show the information that I asked for, then someone can help you further.
Stephen23
on 4 Jun 2022
@Chaudhary P Patel: how do you imagine processing all of those magically-named variables?
Image Analyst
on 4 Jun 2022
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
on 4 Jun 2022
@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
on 4 Jun 2022
Edited: Image Analyst
on 4 Jun 2022
@Steven Lord oh, right. I incorrectly thought it was the original poster asking a follow up question.
Jan
on 4 Jun 2022
Did I mention already, that eval does not only impede the efficient execution of the code but also the clarity of the discussion?
Stephen23
on 5 Jun 2022
"...not only impede the efficient execution of the code but also the clarity of the discussion?"
Indeed: https://xyproblem.info/
Chaudhary P Patel
on 5 Jun 2022
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
on 5 Jun 2022
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
on 5 Jun 2022
@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
on 5 Jun 2022
Edited: Stephen23
on 5 Jun 2022
"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
on 5 Jun 2022
%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
on 5 Jun 2022
@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
on 5 Jun 2022
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
on 5 Jun 2022
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
on 6 Jun 2022
"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);
% ^ ^
Answers (1)
Jan
on 4 Jun 2022
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
See Also
Categories
Find more on Signal Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)