Struct convert to a Indexed values

1 次查看(过去 30 天)
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

回答(1 个)

Jack
Jack 2023-4-3
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.

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by