cell2struc "Number of field names must match number of fields in new structure" error
12 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Ashley Lewis
le 8 Août 2018
Réponse apportée : Kaitlyn Keil
le 8 Août 2018
Hello! I am trying to assign 3 fields to each image of this cell array when I convert it to a structure array. I think the error is occurring since the cell array is 16x1 and therefore expects 1 field along the 2nd dimension. However I am looking to assign three fields for each image (therefore each cell) in the array. How can I change my code to do this? Thanks for any help!
N = 16;
C = cell(1,N);
for k = 1:N
F = sprintf('image%d.bmp',k);
C{k} = imread(F);
C_transposed = C';
end
fields= {'image',[], 'category','', 'Label', ''};
GImageDataManMade = cell2struct(C_transposed, fields,2)
error: Error using cell2struct Number of field names must match number of fields in new structure.
0 commentaires
Réponse acceptée
Kaitlyn Keil
le 8 Août 2018
I believe what you want to do is the following:
N = 16;
C = cell(3,N);
for k = 1:N
F = sprintf('image%d.bmp',k);
C{1,k} = imread(F);
C_transposed = C';
end
fields= {'image','category', 'Label'};
GImageDataManMade = cell2struct(C_transposed, fields,2)
and then set the category and Label separately (or during the loop). Before, it was trying to shape a 16x1 cell array into a structure that wanted 16x6 (the '[]' and '' in fields counted as new fields as well), as you suspected.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Cell Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!