How can i use two variables while using eval command

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 Commenti

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.
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?
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?
Stephen23
Stephen23 il 4 Giu 2022
Modificato: Stephen23 il 4 Giu 2022
"how can i wrote it?"
Indexing.
"i want i and n both syntax"
Then use a matrix with two indices. What sizes are k_t and Uy ?
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,
@Chaudhary P Patel: if you show the information that I asked for, then someone can help you further.
eval("FSS_" + i + "_" + n + "=k_t*Uy(:,1)")
@Chaudhary P Patel: how do you imagine processing all of those magically-named variables?
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?
@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.
@Steven Lord oh, right. I incorrectly thought it was the original poster asking a follow up question.
Did I mention already, that eval does not only impede the efficient execution of the code but also the clarity of the discussion?
"...not only impede the efficient execution of the code but also the clarity of the discussion?"
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.
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.
@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
Stephen23 il 5 Giu 2022
Modificato: Stephen23 il 5 Giu 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.
%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
@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
...
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
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
"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);
% ^ ^

Accedi per commentare.

Risposte (1)

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

Categorie

Commentato:

il 6 Giu 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by