Matlab: adding a value to an initialized nested structural cell

I have this structure

Data = struct('trials',{},'time',{},'theta_des',{},'vel_des',{},'trials_number',{},'sample_numbers',{});
Data(1).trials = cell(1,trials_number);
for i=1:trials_number
   Data.trials{i} = struct('theta',{},'pos_err',{},'vel',{},'vel_err',{},'f_uparm',{},'f_forearm',{},'m_uparm',{},'m_forearm',{},...
                           'current',{},'total_current',{},'control_output',{},'feedback',{},'feedforward',{},'kp',{});
end

but when I want to add a value

Data.trials{i}.theta = 27;

I get this error ...

A dot name structure assignment is illegal when the structure is empty.  Use a subscript on the structure.

Any idea on how to solve it?

Thank!

+4
source share
1 answer

If you look at the documentation struct, it says the following statement:

s = struct(field,value) creates an array of structure with the specified field and values.

...

...

  • If any input valueis an empty array of cells, {}, then the output of s is an empty (0-by-0) structure.

{}, , , , . struct, [] . , for :

for i=1:trials_number
    Data.trials{i} = struct('theta',[],'pos_err',[],'vel',[],'vel_err',[],'f_uparm',[],'f_forearm' [],'m_uparm',[],'m_forearm',[],...
    'current',[],'total_current',[],'control_output',[],'feedback',[],'feedforward',[],'kp',[]);
end

, . , theta :

Data.trials{1}.theta = 27;

. :

disp(Data.trials{1}.theta)

27
+3

All Articles