現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
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 件のコメント
KSSV
2022 年 9 月 26 日
Give the necessary inputs of the function at the place you want to cal.....what is confusing you?
Chaudhary P Patel
2022 年 9 月 26 日
for n=1:1:nf
knt=eval(['Ktts',num2str(n),'l']);
end
Sir, I want to use the knt inside the function.
Just tell me how can i call it in side the function.
Chaudhary P Patel
2022 年 9 月 26 日
sir problem is that when i am calling the knt inside the loop it is telling that Ktts1l is undefined.
Stephen23
2022 年 9 月 26 日
"problem is that when i am calling the knt inside the loop it is telling that Ktts1l is undefined."
What is KTTS1L supposed to be: a function, a script, a variable, an object ... ?
Chaudhary P Patel
2022 年 9 月 26 日
knt=eval(['Ktts',num2str(n),'l']);
Ktts is elemental stiffness in which 1 means first element and l means local. calculated from the knt.
Stephen23
2022 年 9 月 26 日
Please answer this question: what is KTTS1L supposed to be: a function, a script, a variable, an object ... ?
Chaudhary P Patel
2022 年 9 月 26 日
eval(['Ktts',num2str(flno),'l','=[k1,-k1*au;-bu*k1,bu*k1*au]']);
Sir, Ktts is Variable which is storing the elemental stiffness in form of Ktts1l, Ktts2l,.......
Chaudhary P Patel
2022 年 9 月 26 日
Sir, for another operation i have to use that value that's whay i am calling it inside the function.
Sir, it will be a great help if you could help me out from this problem.
Stephen23
2022 年 9 月 26 日
"Ktts is elemental stiffness in which 1 means first element"
So (as KSSV correctly asks), why are you not using indexing?
"Sir, Ktts is Variable which is storing the elemental stiffness in form of Ktts1l, Ktts2l,......."
Please show how you do that.
Chaudhary P Patel
2022 年 9 月 26 日
sir, in first loop i showed how am i calculating the all Ktts.
All these Ktts i am calling for all n=1:10 in second loop which is inside the Function loop.
Sir, for further operation inside the Function loop i need that, that's why i am calling.
Please you suggest me how can i call it.
Stephen23
2022 年 9 月 26 日
"Please you suggest me how can i call it."
The best solution is to use indexing. Get rid of all of those ugly EVALs and use indexing into one array.
Chaudhary P Patel
2022 年 9 月 26 日
Sir, i will do indexing later on but for this i want to complete it with same commands so please suugest if you could because it is large programme so it will take more time to change many things.
Rik
2022 年 9 月 26 日
Since you will completely redesign your code to use indexing, isn't it more effective to first wait for you to rework you code? Perhaps some or most of your problems disappear or become trivial with the changes you make. You would be surprised how often that happens when people switch from eval to arrays...
Chaudhary P Patel
2022 年 9 月 26 日
Sir, that is fine.
I did this function calling before this but this time i getting problem because of this Function loop.
Sir, any how please suggest me how can i call it, rest of things i will manage.
George Papazafeiropoulos
2022 年 9 月 26 日
['Ktts',num2str(n),'l'] must be an input argument to the NewmarkNon function.
Chaudhary P Patel
2022 年 9 月 26 日
編集済み: Chaudhary P Patel
2022 年 9 月 27 日
But how to write this in the input argument.
Stephen23
2022 年 9 月 26 日
"But how to write this in the input argument."
Using exactly the same horrible, complex, inefficient approach you used to generate those variables: using EVAL.
Rik
2022 年 9 月 27 日
You have been told already what you should do: use arrays. Do you expect Stephen to give you advice about implementing a solution with eval? I know him as one of the most vocal opponents of the use of eval, so I don't expect that to happen.
Chaudhary P Patel
2022 年 9 月 27 日
Sir, I just want a help from any person.
Sir,please help me if you can, it will be very helpful for me.
Walter Roberson
2022 年 9 月 27 日
What you want to do cannot be done using eval. The variables assigned to in the script are not available inside the workspace of the function.
The main purpose of using functions is to encapsulate code and variables in a way that does not interfere with the calling code except through the defined interface layer, so if your function needs access to dynamic variables created by a different layer, then it is likely badly designed.
Chaudhary P Patel
2022 年 9 月 27 日
sir, is there any way to proceed this operation without changing the code?
Rik
2022 年 9 月 27 日
Let me try as well: No. There is no way to proceed this operation without changing the code.
You want help. We are giving you help, even if you don't like it. Asking again doesn't change the answer.
Chaudhary P Patel
2022 年 9 月 27 日
Sir, it doesn't mean that I don't like your answer.
I'm sorry if you felt that way.
sir if i will change the code it will eat more of my time so i want to go ahead with your help without changing the code.
Stephen23
2022 年 9 月 27 日
"is there any way to proceed this operation without changing the code?"
No.
採用された回答
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 件のコメント
Bruno Luong
2022 年 9 月 27 日
編集済み: Bruno Luong
2022 年 9 月 27 日
Use eval to call your function and put the ugly ['Ktts',num2str(n),'l'] somewhere in the argment list.
You'll soon end up with code having eval in every lines.
Chaudhary P Patel
2022 年 9 月 27 日
Sir, if i am keeping ['Ktts',num2str(n),'l'] like this in the argument list it is showing error
"Invalid MATLAB Syntax"
Chaudhary P Patel
2022 年 9 月 27 日
Sir, can i use "knt = evalin(['caller','Ktts',num2str(n),'l'])" directly without keeping it in the input argument.
Bruno Luong
2022 年 9 月 27 日
Because you do it wrong I guess. You decide to use eval then then you commit yourself to debug te ugly code.
Bruno Luong
2022 年 9 月 27 日
"Sir, can i use "knt = evalin(['caller','Ktts',num2str(n),'l'])" directly without keeping it in the input argument."
It's your code you do whatever you like? You ask me one way or another, I tell you the hint of doing both.
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
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
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
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 Exchange で Deployment, Integration, and Supported Hardware についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)