How can I use the content of a variable to call another variable?

19 views (last 30 days)
I'm confronted with the following problem:
eur2usd =[ 1.3 1.2 1.1];
eur2cad =[0.8 0.9 0.7];
currency = 'eur2usd'
I would like to use the variable currency to call the content of eur2usd ([ 1.3 1.2 1.1]). So that if currency = 'eur2cad', I can call the content of eur2cad by using the variable currency.
Stephen23 on 4 Jan 2015
Basically you should not do this. Using dynamically defined variable names or encoding data within the variable name is a pretty bad idea in MATLAB, as is described on many threads on MATLAB Answers:
The first of these links gives an excellent alternative, which is what you should probably be using for your data: structures. This shows how it could work:
>> A = struct('eur2usd',[ 1.3 1.2 1.1],'eur2cad',[0.8 0.9 0.7]);
>> A.('eur2cad')
ans = [0.8,0.9,0.7]
Note that using a structure is extendable to any number of currencies without cluttering-up your workspace with a thousand variables. There is also a large selection of tools that you can use to manipulate structures, their fields and contents:
Summary: do not use eval to encode the data into the variable names!

Sign in to comment.

Accepted Answer

Andrew Reibold
Andrew Reibold on 18 Aug 2014
Edited: Andrew Reibold on 18 Aug 2014
Stephen23 on 11 Mar 2015
Although this answer has been accepted, avoid using eval and do not create dynamically named variables in MATLAB. This is poor practice as has been explained many times on this forum, and is not recommended by MATLAB themselves:
When you are a beginner it seems like a cunning and fast way to store information, but actually it is really bad practice to name your variables dynamically. MATLAB is also not intended for this kind of variable naming: if you continue to include data in the variable names then you will find yourself fighting many more of these battles against MATLAB.
However when you use more appropriate storage for your data (and meta-data) then you will suddenly find lots of MATLAB functions that do many useful operations for you, quickly and easily.
In your case a much more robust solution would be to use structures , where you can include fields for each kind of data (e.g. Process type, Flow data, Temperature data, Notes, Units, etc), or cell arrays . There are many functions that support working on structures and cell arrays, and can access these data easily, and they can also be used in vectorized code (which is something you need to learn about). And yes, you can even define structure fieldnames dynamically .
Placing your data in a structure or cell array also makes it much easier to pass to functions: can you imagine the fight you would have trying to pass hundreds of dynamically named variables to a function?
If you have a newer version of matlab you can also use a table , which stores the data together in one array but also allows key-name access to the columns. This might be a good alternative for your data.
In case you are interested, here are some pages explaining why dynamically assigning variable names is a really bad idea in MATLAB:

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by