Creating an array of structure dynamically without eval

I am trying to create an array of structure at runtime in Matlab.

    A= {'dark';'oa_45'; 'oa_225'};

    for i = 1:3
      tmp =load([folder '/' A{i} '.txt']);
      eval([A{i} '.count=tmp(:,1:2)']);
      eval([A{i} '.mean=mean(tmp(:,1:2),1)']);
      eval([A{i} '.sqrtmean=sqrt(' A{i} '.mean)']);
      eval([A{i} '.stdev=std(tmp(:,1:2),1)']);
      eval(A{i});
    end

Since I know that using evalis a pretty bad practice, I would like to know if there is an easy way to avoid evalhere.

I thought it was possible to create an array of the structure before the loop, and then assign only subordinate fields in the loop with the notation in brackets:

   s.(A{i}).count = ...

I found here a few suggestions that say this is possible with subsasgn. It seemed more complex than a function eval.

Does anyone know an easy way to escape a function eval, or is this the best call here? I'm just asking out of curiosity, I think for these three vectors the loss of performance doesn't really matter.

Best regards, Mechanix

+4
2

, , "", "oa_45", "oa_225" "sqrtmean", , -

1 ()

A= {'dark';'oa_45'; 'oa_225'};
fieldnames1 = {'count';'mean';'stdev'};
funcnames1 = {'';'mean';'std'};

for k = 1:numel(A)
    tmp =load([folder '/' A{k} '.txt']);
    struct1 = A{k};
    for i = 1:numel(fieldnames1)
        if isempty(funcnames1{i})
            comp_struct.(struct1).count=tmp(:,1:2);
        else
            fh = str2func(funcnames1{i});
            comp_struct.(struct1).(fieldnames1{i}) = fh(tmp(:,1:2),1);
        end
    end
    comp_struct.(struct1).sqrtmean = sqrt(comp_struct.(struct1).mean);
end

, "comp_struct.dark" "" ..

IF-ELSE -

2 ()

A= {'dark';'oa_45'; 'oa_225'};
fieldnames1 = {'count';'mean';'stdev'};
funcnames1 = {'donothing';'mean';'std'};

for k = 1:numel(A)
    tmp =load([folder '/' A{k} '.txt']);
    struct1 = A{k};
    for i = 1:numel(fieldnames1)
        fh = str2func(funcnames1{i});
        comp_struct.(struct1).(fieldnames1{i}) = fh(tmp(:,1:2),1);
    end
    comp_struct.(struct1).sqrtmean = sqrt(comp_struct.(struct1).mean);
end

-

function out = donothing(varargin)
out = varargin{1};

, EVAL , , , , -

3 ( )

A= {'dark';'oa_45'; 'oa_225'};
for k = 1:numel(A)
    tmp =load([folder '/' A{k} '.txt']);
    comp_struct.(A{k}).count = tmp(:,1:2);
    comp_struct.(A{k}).mean = mean(tmp(:,1:2),1);
    comp_struct.(A{k}).sqrtmean = sqrt(comp_struct.(A{k}).mean);
    comp_struct.(A{k}).stdev = std(tmp(:,1:2),1);
end
+2

" ", 3 ! :

A = {'dark';'oa_45'; 'oa_225'};

% Preallocation is obviously optional, but good practice
% A cell array initialiser will make structarray the same size
structarray = struct('name',A,'count',[],'mean',[],'sqrtmean',[],'stdev',[]);

for i = 1:length(A)
  tmp = load([folder '/' A{i} '.txt']);
  % structarray(i).name = A{i};   % if we didn't preallocate
  structarray(i).count = tmp(:,1:2);
  structarray(i).mean = mean(tmp(:,1:2),1);
  structarray(i).sqrtmean = sqrt(structarray(i).mean);
  structarray(i).stdev = std(tmp(:,1:2),1);
end

eval , , , .

, , - , , . , , , -, - , . , , , . , , , name .

0

Source: https://habr.com/ru/post/1530796/


All Articles