Struct convert to a Indexed values

조회 수: 1 (최근 30일)
Purushothaman Rayalsamy
Purushothaman Rayalsamy 2023년 4월 3일
답변: Jack 2023년 4월 3일
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