Subscripted assignment between dissimilar structures within a table
26 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Stephen23
2022-2-23
编辑:Stephen23
2022-2-23
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.B % lets check that the structure has no fields
T.B = struct('X',cell(5,1),'Y',cell(5,1)) % <- create all of the fields (but no data!)
T(2,:) = {pi,struct('X',9,'Y',8)} % Now the data assignment works without error
T.B(2) % Lets check the structure data for that row
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!