Struct convert to a Indexed values
3 views (last 30 days)
Jack on 3 Apr 2023
To convert a struct with 301 rows, each containing a 36x1 Target information, into a struct with 301 indexes and 36 columns for the Target information, you can use the following code:
% Let's assume your original struct is called 'struct_with_301_rows'
% Preallocate a struct with 36 fields
struct_with_36_columns = struct('Target', cell(36, 1));
% Loop through each row of the original struct
for i = 1:numel(struct_with_301_rows)
% Get the Target information for this row
target_info = struct_with_301_rows(i).Target;
% Loop through each element of the Target information and assign it to
% the corresponding field of the new struct
for j = 1:numel(target_info)
struct_with_36_columns(j).(sprintf('Target_%03d', i)) = target_info(j);
This will create a new struct struct_with_36_columns with 36 fields, each containing the Target information for all 301 rows. The field names are generated automatically and follow the format Target_XXX, where XXX is the index of the original struct row (e.g. Target_001, Target_002, etc.). Note that if your original struct contains fields other than Target, you will need to modify the code accordingly.