Azzera filtri
Azzera filtri

Struct convert to a Indexed values

3 visualizzazioni (ultimi 30 giorni)
Purushothaman Rayalsamy
Purushothaman Rayalsamy il 3 Apr 2023
Risposto: Jack il 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

Risposte (1)

Jack
Jack il 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.

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by