define variable from cell content
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi!
I have a problem with cell contents...
I have 2 cells
C1={'abc','def','ghi'}
C2={'1','2','3'}
I want to create a variable from the content of C1 with the value of C2.
abc=1
def=2
ghi=3
Those variables should be created automatically within a loop, because the original cells are bigger than 3 values.
I hope someone can help me with that problem. Thank you very much!
1 Kommentar
Stephen23
am 29 Mär. 2021
What happens if one of the strings in C1 is '123' or 'quit' or 'sum' or 'max' or '1+2' or '#time' ?
Antworten (1)
Stephen23
am 29 Mär. 2021
Simpler and much more efficient than what you are attempting is to use a basic structure:
C1 = {'abc','def','ghi'};
C2 = {'1','2','3'};
S = cell2struct(num2cell(str2double(C2)),C1,2);
S.abc
S.ghi
4 Kommentare
Rik
am 29 Mär. 2021
The flaw is with the software that requires this. What is this strange software that needs variables to have a specific name? Why isn't that a function (which wouldn't care about the variable name in the caller)?
The code you show is one of two way I know. The other way would be to store to a struct, saving that to a mat file with the '-struct' flag and loading without an output to poof the variables into existence. You should use neither.
The solution is to change that script you're calling to a function. If you make a brigde out of spaghetti, the solution is not to use water-repelling glue, the solution is to switch to steel.
Siehe auch
Kategorien
Mehr zu Function Creation 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!