string - Matlab cells with Names -
note: please if can point solution without 'eval', great!!! if not, i'll thankful :-) well, have cell (var_s
) has in first row strings , in second row matrices:
clc clearvars fclose l=[11 22 33 44]; m=[1 2 3]; n=[101 102 103 104 105, 95 96 97 98 99]; var_s=cell(2,3); var_s(1,1:3)={'rn', 'le', 'o'}; %// strings different , not created in loop. var_s(2,1:3)={l, m, n}; %// no correlation possible. %//than delete variables because can work cell (var_s{2,:}) %//to execute computations clearvars l m n %//now want save values stored inside of cells in %//second line of var_s, var_s{2,:}, associating them names in first %//row of var_s, var_s{1,:}, in .mat file %//but let's imagine instead of having size(var_s{2,:})=3 have %//something 1000 (but lets keep simple having 3). %//consequently want use 'for' work! %//and @ point issue appears. how can use strings %//inside var_s{1,:} create variables , store them in .mat file %//the values in cells of var_s{2,:} associated them? filename='clima'; a=0; save(filename,'a'); %//creats file variable a=0 i=1:length(var_s(2,:)) genvarname(var_s{1,i})=var_s{2,i}; %//the idea create variable using stringn , associate values save(filename,char(var_s{1,i}),'-append'); %//the idea add created variable has same name in var_s{1,i} end clearvars %//after access variables stored in 'clima.mat' %//by name such load('clima.mat') rn le o
and result must
rn = 11 22 33 44 le = 1 2 3 n = 101 102 103 104 105
your question pretty covered in docs save()
command under "save structure fields individual variables". there, must create struct
.
to create struct()
, dynamically create field names, not of code must changed. once struct created in loop, save struct once after loop option '-struct'
, automatically generates new variable each field in struct.
s = struct(); i=1:length(var_s(2,:)) s.(var_s{1,i})=var_s{2,i}; % struct dynamic field names end save(filename, '-struct', 's');
now let's see, stored:
whos('-file', 'clima.mat') name size bytes class attributes le 1x3 24 double o 1x10 80 double rn 1x4 32 double
as can see, stored 3 variables in file.
Comments
Post a Comment