How can I call a value in side the a function which is already evaluated.
Show older comments
%% 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 Comments
KSSV
on 26 Sep 2022
Give the necessary inputs of the function at the place you want to cal.....what is confusing you?
Chaudhary P Patel
on 26 Sep 2022
Chaudhary P Patel
on 26 Sep 2022
Stephen23
on 26 Sep 2022
"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
on 26 Sep 2022
Chaudhary P Patel
on 26 Sep 2022
Stephen23
on 26 Sep 2022
Please answer this question: what is KTTS1L supposed to be: a function, a script, a variable, an object ... ?
Chaudhary P Patel
on 26 Sep 2022
Rik
on 26 Sep 2022
Why are you generating variables, instead of using indexing?
Chaudhary P Patel
on 26 Sep 2022
Stephen23
on 26 Sep 2022
"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
on 26 Sep 2022
Stephen23
on 26 Sep 2022
"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
on 26 Sep 2022
Rik
on 26 Sep 2022
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
on 26 Sep 2022
Chaudhary P Patel
on 26 Sep 2022
George Papazafeiropoulos
on 26 Sep 2022
['Ktts',num2str(n),'l'] must be an input argument to the NewmarkNon function.
Chaudhary P Patel
on 26 Sep 2022
Edited: Chaudhary P Patel
on 27 Sep 2022
Stephen23
on 26 Sep 2022
"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.
Chaudhary P Patel
on 27 Sep 2022
Rik
on 27 Sep 2022
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
on 27 Sep 2022
Walter Roberson
on 27 Sep 2022
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
on 27 Sep 2022
Rik
on 27 Sep 2022
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
on 27 Sep 2022
Stephen23
on 27 Sep 2022
"is there any way to proceed this operation without changing the code?"
No.
Chaudhary P Patel
on 27 Sep 2022
Accepted Answer
More Answers (1)
Walter Roberson
on 26 Sep 2022
0 votes
It is not possible to do what you want to do using eval(). You will need to rewrite your code.
2 Comments
Walter Roberson
on 26 Sep 2022
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
on 26 Sep 2022
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.
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!