0

I am trying to assign the field values of structure in loop.

Structure declaration with empty values:

result_struct = struct('a',{},'b',{},'c',{},'d',{})

I am assigning values in loop like that:

% assume a, b, c, d are variables in my workspace

% field names match with the variable names

for index=1:n

% some computation and store results in variables (a-d)

  result_struct(index).a = a;

  result_struct(index).b = b;

  result_struct(index).c = c;

  result_struct(index).d = d;

end

How can I assign the values to the fields using another loop? Like that:

for fname = fieldnames(result_struct)'

result_struct(index).fname = fname; % field names and variable names match

end
Suever
  • 64,497
  • 14
  • 82
  • 101
asif
  • 3
  • 2

1 Answers1

0

You need to use dynamic field names to assign to the struct (the leff-hand side). For the right hand side you could use eval but that is dangerous, so it's better to save your variable fname to a file and then load it back in as a struct prior to accessing fname, again using dynamic field names.

names = fieldnames(result_struct);    

for k = 1:numel(names)
    % Save variable to a file
    save('tmp.mat', names{k});        

    % Load it back into a struct
    tmp = load('tmp.mat', names{k});

    result_struct(index).(names{k}) = tmp.(names{k});
end

Alternately, you can use the save and load to just transform the entire thing into a struct without having to loop through the fields.

fields = fieldnames(result_struct);

% Save all relevant variables to a file
save('tmp.mat', fields{:});

% Load it back into the result_struct
result_struct(index) = orderfields(load('tmp.mat'), fields);
Suever
  • 64,497
  • 14
  • 82
  • 101
  • Hi Suever, thanks for the reply. Actually I got MATLAB error when I try 2nd method (without loop). The error says "Subscripted assignment between dissimilar structures". When I load to new structure, it gets loaded without problem. What I observed is the order of the fields is different. Does it play a role? – asif Mar 03 '17 at 14:08
  • This code works: clear all; close all; result_struct = struct('a',{},'b',{},'c',{},'d',{}); result_index = 1; fields = fieldnames(result_struct) for a = 1:3 for b = 1:3 c = a+b; d = a-b; save('tmp.mat', fields{:}); result_struct(result_index) = load('tmp.mat'); result_index = result_index + 1; end end result_table = struct2table(result_struct) – asif Mar 03 '17 at 14:36
  • @asif Updated to ensure that the fields have the same ordering. – Suever Mar 03 '17 at 14:38
  • I think, the problem lies in the ordering of the variables. In my real example, I have defined structure with the following fields in this order: vin, fet_length, vout, iout, fsw, nfet_wide, ratio_width. And I get subscripted assignment between dissimilar structures error. Any help will be appreciated! – asif Mar 03 '17 at 14:46
  • matlab code gets extremely slow when file IO is involved --> understood, but the execuation time is in hours. I am generating 50k rows of data with 50 columns... – asif Mar 07 '17 at 07:23