Use text in table to make variables
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
David Slater
am 22 Jan. 2021
Kommentiert: David Slater
am 23 Jan. 2021
I have a table, T; the first column contains text enclosed in single quotes, and the second column contains scalar numbers:
Symbol Value
1 'Z1' 3
2 'Z2' 4
How can I get Matlab to create variables from the Symbols and assign the Values to them? ie
Z1=3
Z2=4
I could convert T.Symbol into a string vector:
T.Symbol =s,
but how can I convert s into variables, without having to type each variable manually? (In reality, my table contains many rows, and I have more than one table.)
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 22 Jan. 2021
That is not recommended. For example, how would you expect your code to be able to proceed if one of the symbols was 'T' and so you assigned a numeric value to T, overwriting T's usage as a table?
T = table({'Z1'; 'Z2'}, [3;4], 'VariableNames', {'Symbol', 'Value'})
vars = cell2struct(num2cell(T.Value), T.Symbol, 1)
vars.Z2
Weitere Antworten (1)
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!