How can i use two variables while using eval command

3 visualizaciones (últimos 30 días)
Chaudhary P Patel
Chaudhary P Patel el 4 de Jun. de 2022
Comentada: Stephen23 el 6 de Jun. de 2022
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 comentarios
Walter Roberson
Walter Roberson el 5 de Jun. de 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
Stephen23 el 6 de Jun. de 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);
% ^ ^

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 4 de Jun. de 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

Categorías

Más información sobre Signal Processing Toolbox en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by