How to make dynamic variable names (A1, A2, A3, ..., ) with "for" loop?

조회 수: 53 (최근 30일)
abdelkrim bensmaine
abdelkrim bensmaine 2021년 12월 15일
댓글: abdelkrim bensmaine 2021년 12월 15일
Hello community,
my knowledge of Matlab is limited, I admit it. So, I am sorry if I am going to make mistakes.
I have to create a series of variable using a "for" loop to associat it with a TF in order to draw Bode diagram for each delay,I tried this but it didn't work:
num = [1];
dem = [1 1 0 0];
T=[0.1,0.5,2,3,4,20];%% delay
% ind='A,B,C,D,E,F';
% index=strsplit(ind,',') I tried to make a variable with index(j) but
% it didn't work for me
for i=1:length(T)
for j=1:length(T)
eval(['A' num2str(j) ])=tf(num ,dem,'Inputdelay',T(i));
for plotId = 1 : 6
subplot(3,2,plotId), bode(A(j))
grid on;
title(['delay=',num2str(T(plotId))])
end
end
end

채택된 답변

Rik
Rik 2021년 12월 15일
Don't use numbered variables. Use cell arrays instead. I also changed your loops to use n instead of i and j (as they can be cofused for sqrt(-1)) and replace length with numel, as that is probably what you meant.
Since your three loops seemed to do the same thing, I merged them.
num = [1];
dem = [1 1 0 0];
T=[0.1,0.5,2,3,4,20];%% delay
% ind='A,B,C,D,E,F';
% index=strsplit(ind,',') I tried to make a variable with index(j) but
% it didn't work for me
for n=1:numel(T)
A{n}=tf(num ,dem,'Inputdelay',T(n));
subplot(3,2,n), bode(A{n})
grid on;
title(sprintf('delay=%.1f',T(n)))
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by