Getting a variable and its value from a csv file

54 views (last 30 days)
Hi,
i got a csv file with several colums in the form of
a parameter 1
b species 2
c parameter 3
etc. For now i am using
datastart = readtable('input_parameter_example.csv');
ds = datastart{1:128,{'value'}};
var1=str2double(cell2mat(ds(1,1)));
which gets the number value for a specific column from the table, i set the name and attach the value by using str2double(cell2mat()). But thats not what i want in the end. What i want is that this happens automatically, so i want to read from the table that the paramater named "a" has the value "1", and i want to use this parameter later on in a another script. So i want to read dynamically the variable name and its value, and when i call the script, the variable and its value should be known. I did google a lot but cannot find a solution.
Thanks

Answers (2)

KSSV
KSSV on 18 Jun 2019
T = readtable(file) ;
T.var % where var is your variable name
  1 Comment
Walter Roberson
Walter Roberson on 18 Jun 2019
This does not accomplish,
"i want to read from the table that the paramater named "a" has the value "1", and i want to use this parameter later on in a another script. So i want to read dynamically the variable name and its value"

Sign in to comment.


Nicolas Fischer
Nicolas Fischer on 19 Jun 2019
Thanks for the answers. So there is no way, if i got a csv (or any other format data file) where in the first column the variable name and in the 2nd column the variable value is stated, that i just call both informations in Matlab from the table and get eg a=1 so that the program knows there is an integer variable a with the value 1?
  2 Comments
Walter Roberson
Walter Roberson on 19 Jun 2019
Yes, there are ways. However, it is almost always a bad idea to use them.
For example, suppose that the file contains an entry that defines a value for clear . Then after that, it would be difficult to clear any variable or function, because you would want that in every function, clear would now refer to that integer value instead of to the command.
Stephen23
Stephen23 on 19 Jun 2019
"So there is no way,"
There are ways, but using them will force you into writing slow, complex, obfuscated code that is liable to bugs and also be hard to debug. The best** solution would be to use a structure or table, as KSSV showed.
** "best" in the sense neat, less liable to bugs, easier to debug, and most efficient.

Sign in to comment.

Categories

Find more on Tables 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!