I have the following example which expresses the type of problem that I’m trying to solve:
clear all
textdata = {'DateTime','St','uSt','Ln','W'};
data = rand(365,4);
Final = struct('data',data,'textdata',{textdata})
clear textdata data
From this, Final.data contains values which correspond to the headings in Final.textdata excluding the first (‘DateTime’) thus Final.data(:,1) corresponds to the heading ‘St’… and so on. What I’m trying to do is to create a variable in the workspace for each of these vectors. So, I would have a variable for St, uSt, Ln, and W in the workspace with the corresponding values given in Final.data.
How could this be done?
The direct answer to your question is to use the
assigninfunction, like so (edit: just like macduff suggested 10 minutes ago):However, I strongly discourage using dynamic variable names to encode data like this. Code that starts this way usually ends up as spaghetti code full of long string concatenations and
evalstatements. Better is to use a structure, like thisOr, to get the same result in a single line: