Subscripted assignment between dissimilar structures within a table

20 vues (au cours des 30 derniers jours)
Jordan Wiker
Jordan Wiker le 23 Fév 2022
Commenté : Jordan Wiker le 24 Fév 2022
I'm trying to populate a table that includes a column of 1x1 structs. First I create the table as follows:
tableSize = [500 10];
varNames = ["Name","LocationName","LLA","SystemType","PlatformType",...
"Antenna","Rx","Tx","Gain_dBi","AdditionalInfo"];
varTypes = ["string","string","cell","string","string",...
"string","logical","logical","double","struct"];
rxTable = table('Size',tableSize,'VariableTypes',varTypes,'VariableNames',varNames)
rxTable =
500×10 table
Name LocationName LLA SystemType PlatformType Antenna Rx Tx Gain_dBi AdditionalInfo
_________ ____________ ____________ __________ ____________ _________ _____ _____ ________ ______________
<missing> <missing> {0×0 double} <missing> <missing> <missing> false false 0 1×1 struct
rxTable(1,:).AdditionalInfo
ans =
struct with no fields.
Then, after defining all the inputs, I try to populate a row as follows:
additionalInfo = struct('URL','www.google.com', 'SNR', 10, 'NumElements', 1, 'NumChannels', 1);
rxTable(i,:) = {name, locationName, LLA, systemType, platformType, antenna,...
Rx, Tx, gain, additionalInfo};
All the variables are successfully populated as expected except the struct in the last column. It gives me the following error
Subscripted assignment between dissimilar structures.
I'm guessing it has to do with trying to replace the initially defined empty 1x1 struct with a populated 1x1 struct, but I can't figure out how to get arround it.

Réponse acceptée

Stephen23
Stephen23 le 23 Fév 2022
Modifié(e) : Stephen23 le 23 Fév 2022
A 1x1 structure is not empty, it is scalar: its size is independent of how many fields it has.
You are trying to assign a structure with fields to elements of a structure with different fields (i.e. none). That is not allowed. However you can replace the structure array with one containing the required fields:
T = table('Size',[5,2],'VariableTypes',["double","struct"],'VariableNames',["A","B"])
T = 5×2 table
A B _ __________ 0 1×1 struct 0 1×1 struct 0 1×1 struct 0 1×1 struct 0 1×1 struct
T.B % lets check that the structure has no fields
ans = 5×1 struct array with no fields.
T.B = struct('X',cell(5,1),'Y',cell(5,1)) % <- create all of the fields (but no data!)
T = 5×2 table
A B _ __________ 0 1×1 struct 0 1×1 struct 0 1×1 struct 0 1×1 struct 0 1×1 struct
T(2,:) = {pi,struct('X',9,'Y',8)} % Now the data assignment works without error
T = 5×2 table
A B ______ __________ 0 1×1 struct 3.1416 1×1 struct 0 1×1 struct 0 1×1 struct 0 1×1 struct
T.B(2) % Lets check the structure data for that row
ans = struct with fields:
X: 9 Y: 8

Plus de réponses (0)

Catégories

En savoir plus sur Structures dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by