how can i assign a variable existing in the workspace to a char ?

Hello everyone,
how can i assign a variable existing in the workspace to a char ?
For example, in my workspace i have: label = 1 (double)
in a script i have a variable X = 'label' (char), so i want it to take the value of the variable label so that X = label = 1.
Is it possible to do such thing ?
Thanks alot for your help

 Accepted Answer

label=1;
X='label';
y=evalin('base',X);
And wait for comments critizing this method.

2 Comments

Here are the expected comments, which critize this method: Don't do this, because it increases the complexity of the code even further. See TUTORIAL: How and why to avoid Eval
:-)
Ok, let's do it the right way. Assume the following data in an Excel file. Use table so any value can be referenced by T{X,Y}
name value threshold
label1 1 10
label2 2 20
label3 3 30
T=readtable('book1.xlsx','ReadRowNames',true)
X='label2';
Y='threshold';
T{X,Y}

Sign in to comment.

More Answers (1)

Use a struct and dynamic field names:
% In the current workspace:
Data.label = 1;
Result = YourFunction(Data);
function Result = YourFunction(Data)
Field = 'label';
Result = (Data.(Field) + 1) ^ 2;
end
Remember, that it is a DON'T to access variables dynamically: See TUTORIAL: How and why to avoid Eval
But fieldnames are efficient and clean.

5 Comments

Hello Jan,
Thank you for your reply.
The problem is that i extract the variable 'label' from an xls sheet and i do it automatically so i can't see how i can use a structure in this case ...
For instance, i have this in my xls sheet called "list":
name value threshold
'label1' 1 10
'label2' 2 20
'label3' 3 30
I managed to extract all the variables into my workspace so i have then in double format.
Next, i use this function to do a simple comparison:
function y = thd_equal(label,thd)
if(label == thd)
y = true;
else
y = false;
end
end
So when i call this function, i use this in a loop:
result(i,1) = thd_equal(list{i,1},list{i,2})
So i give the function the value of list{i,1} which is a char, and thus i want him to take the value of this char linked with the existing variable in the workspace.
Thanks again
The problem is that you assigne a variable "automatically". As explained exhaustively, this is a shot in your knee. Finding even more complicated to handle this too complicated mistake is not efficient, but recreate the code, such that the access of the variables is trivial.
The suggested evalin works, and it increases your chance to create a code, which is such complicated, that you cannot debug it anymore.I've seen too many codes, which cannot be maintained due to such design errors. Please read the suggested tutorial again, until you understand the meaning. The topic has been discussed several hundrets times with newcomers and the result was the same every time: Prefer a clean programming style without dynamic access of variables.
By the way, this function:
function y = thd_equal(label,thd)
if(label == thd)
y = true;
else
y = false;
end
end
does exactly the same as:
label == thd
which is much shorter.
I think using table would solve your problem.
doc table
doc readtable
@Daher: "I managed to extract all the variables into my workspace" - I assume the problem is here. It would be much easier to import the strings as a list and the data as a matrix. Then the access is easy: with the strcmp you find the corresponding row directly.
Fangjun Jiang's idea hits the point also: With importing the data as a table object, the access of the named rows is trivial also. Importing the data as a struct is equivalent, but more handmade.
Hello guys,
thanks again for your replies,
@Fangjun Jian : i see no difference using these commands as i use xlsread ...
Anyway, the evalin command works fine for my case so i think i'll use it.
Thanks for the help guys !

Sign in to comment.

Categories

Products

Release

R2016b

Community Treasure Hunt

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

Start Hunting!