Home > front end >  How to get the value assigned to a variable inside a variable in MATLAB?
How to get the value assigned to a variable inside a variable in MATLAB?

Time:08-09

A1, A2,... are assigned to t. And A1, A2, A3,.... are further have values as assigned in below for loop. Now when I am printing print(t(1,:)), I want the answer 2, but I am getting A1. This is just a dummy code of my actual problem, the A1, A2 actually having 500 x 1 values.

t=['A1';'A2';'A3';'A4';'A5'];
for j=1:length(t)
    eval([t(j,:), '= 2*j'])
end

Can anyone help out how to deal with this problem in eval.

CodePudding user response:

Do not create variables with dynamic names like this. It will cause you to write slow obfuscated code. It is better to use arrays, cell arrays, or struct variables. Read "TUTORIAL: Why Variables Should Not Be Named Dynamically (eval)" on MATLAB Answers for a detailed discussion.

CodePudding user response:

eval(['t(',num2str(j),',:)= 2*',num2str(j)])
  • Related