How can I call a value in side the a function which is already evaluated.

1 回表示 (過去 30 日間)
%% knt i calculated here.
nf=max(elem(:,4));
maxfl=max(elf(:,1));
for flno = 1:1:maxfl
for i=elf(1,1)
for j=1:1:nf
p=CGx(j)-CGx(i);
au=[1,0,0;0,1,p;0,0,1];
bu=(au)';
kse=eval(['KSE',num2str(flno),'l']);
k1=kse(1:nodof,1:nodof);
eval(['Ktts',num2str(flno),'l','=[k1,-k1*au;-bu*k1,bu*k1*au]']); %Transformed Stiffness matrix of each floor (6x6)
end
end
end
%% I have to call the knt inside the loop, how can i call please suggest me.
function [t,k_hat, R, keyp, key,Keyp,Key, delu, k_T, delF_hat, deludot, u0, udot0, uddot0] = NewmarkNon( t,Ug, delF, dt, u_t, u_c,F1, KGf, C, MGf, Rt, Rc, R, gamma, beta, key, k_T,u,udot,uddot)
knt=eval(['Ktts',num2str(n),'l']);
end
  29 件のコメント
Stephen23
Stephen23 2022 年 9 月 27 日
"is there any way to proceed this operation without changing the code?"
No.
Chaudhary P Patel
Chaudhary P Patel 2022 年 9 月 27 日
Thank you so much sir.

サインインしてコメントする。

採用された回答

Bruno Luong
Bruno Luong 2022 年 9 月 27 日
If you want to retrieve value of a variable from the parent workspace inside a function without passing it as argument onstead of normal eval do this
knt = evalin('caller','Ktts',num2str(n),'l'])
  8 件のコメント
Chaudhary P Patel
Chaudhary P Patel 2022 年 9 月 27 日
ok sir.
Thank you so much.
Walter Roberson
Walter Roberson 2022 年 9 月 27 日
Postponing fixing your code by locking in using a hack like this is just going to result in it taking even longer to fix your code later.

サインインしてコメントする。

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 9 月 26 日
It is not possible to do what you want to do using eval(). You will need to rewrite your code.
  2 件のコメント
Walter Roberson
Walter Roberson 2022 年 9 月 26 日
Do not store variables like KSE3l -- use a cell array like KSEl{n} instead. Or possibly a multidimensional array.
Likewise do not store into Ktts*l -- use a cell array like Kttsl{n} instead.
%% knt i calculated here.
nf=max(elem(:,4));
maxfl=max(elf(:,1));
for flno = 1:1:maxfl
for i=elf(1,1)
for j=1:1:nf
p=CGx(j)-CGx(i);
au=[1,0,0;0,1,p;0,0,1];
bu=(au)';
kse = KSEl{flno};
k1 = kse(1:nodof,1:nodof);
Kttsl{flno} = [k1,-k1*au;-bu*k1,bu*k1*au]']); %Transformed Stiffness matrix of each floor (6x6)
end
end
end
When you call your function (not shown in your code) pass in Kttsl as well.
%% I have to call the knt inside the loop, how can i call please suggest me.
function [t,k_hat, R, keyp, key,Keyp,Key, delu, k_T, delF_hat, deludot, u0, udot0, uddot0] = NewmarkNon( t,Ug, delF, dt, u_t, u_c,F1, KGf, C, MGf, Rt, Rc, R, gamma, beta, key, k_T, u, udot, uddot, Kttsl)
n = something appropriate
knt = Kttsl{n};
end
Walter Roberson
Walter Roberson 2022 年 9 月 26 日
Please read http://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval for information about why we strongly recommend against creating variable names dynamically.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDeployment, Integration, and Supported Hardware についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by