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
2019-6-18
T = readtable(file) ;
T.var % where var is your variable name
1 个评论
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
2019-6-19
2 个评论
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
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!