Getting a variable and its value from a csv file

55 次查看(过去 30 天)
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

回答(2 个)

KSSV
KSSV 2019-6-18
T = readtable(file) ;
T.var % where var is your variable name
  1 个评论
Walter Roberson
Walter Roberson 2019-6-18
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"

请先登录,再进行评论。


Nicolas Fischer
Nicolas Fischer 2019-6-19
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 个评论
Walter Roberson
Walter Roberson 2019-6-19
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 2019-6-19
"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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by