Clear Filters
Clear Filters

Struct convert to a Indexed values

2 views (last 30 days)
Purushothaman Rayalsamy
Purushothaman Rayalsamy on 3 Apr 2023
Answered: Jack on 3 Apr 2023
How to convert a Struct(301x1), and each row contains the 36x1 long Target information to a struct with 301 indexes and 36 columns for the Target information

Answers (1)

Jack
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);
end
end
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.

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!