I have a cellarray whose values are used to initialize corresponding structs.
cellarr = {'NI' ; 'EQ' ; 'TA' } ;
defstr = struct('Raw', '-1') ;
for i = 1:size(cellarr,1)
eval([cellarr{i,1} '= defstr;']) %Yes,I know eval is bad!Any other approach?
end
New values are then filled into the Raw field.
dataCell = [] ;
for i=1:size(cellarr,1)
rawCell = [cellarr{i} '.Raw'] ;
dataCell = strcat(dataCell, ', ', rawCell) ;
end
dataCell(1) = [] ;
DESIRED STATEMENT NOW --> [NI.Raw,Eq.Raw,TA.Raw] = filldata()
function[a1,a2,a3] = filldata(), a1 = 1 ; a2 = 2 ; a3 = 3 ; end
I am not able to execute the desired statement, even by using eval. Shall appreciate your help. filldata output count would match that of LHS of desired statement. Thanks.
Here is a possible solution for EVAL:
As in any other interpreted language, the use of EVAL is not best practice (but you seem to already know that).
Unless you specifically need to have variables whose names can only be determined at runtime, I would use cell-arrays or array of structures instead (depending on your needs):