Trouble using container with cell type for key and structure array for values..
조회 수: 6 (최근 30일)
이전 댓글 표시
Given this table,
TestTable_valid =
11×6 table
SerialNumber ACCurrentPreSyncEn ACCurrentPostSyncEn PassFail Model LCRSwept_Yes_No
____________________ __________________ ___________________ __________ _________ _______________
{'A017441206210001'} {'7.1A'} {'7.1A'} {'Passed'} {'VA2C' } {'Yes'}
{'A017441229220004'} {'7.0A'} {'8.6A'} {'Passed'} {'VA2C' } {'Yes'}
{'A020621202230003'} {'0A' } {'7.7A'} {'Failed'} {'VA2CE'} {'Yes'}
{'A020621203230003'} {'0A' } {'7.6A'} {'Failed'} {'VA2CE'} {'Yes'}
{'A020621207230002'} {'0A' } {'7.1A'} {'Failed'} {'VA2CE'} {'Yes'}
{'A020621208230001'} {'0A' } {'7.1A'} {'Failed'} {'VA2CE'} {'Yes'}
{'A020621210230006'} {'1.2A'} {'0.7' } {'Failed'} {'VA2CE'} {'Yes'}
{'A020621215230003'} {'0A' } {'0A' } {'Failed'} {'VA2CE'} {'Yes'}
{'A020621222230004'} {'0A' } {'0A' } {'Failed'} {'VA2CE'} {'Yes'}
{'A020622108240013'} {'7.1A'} {'0A' } {'Passed'} {'VA2CE'} {'Yes'}
{'A020622109240009'} {'7.1A'} {'7.1A'} {'Passed'} {'VA2CE'} {'Yes'}
I'm trying to create a container that uses the serial number as the key, and returns the other columns:
serials = TestTable_valid.SerialNumber
dataValues = TestTable_valid(:, 2:6)
dataStructArray = table2struct(dataValues)
% Create map: key = serial number, value = table row with C–G
lookupMap = containers.Map(serials, dataStructArray, 'UniformValues', false); % needed because data types in structure may be different.
however, I get the following error:
Error using containers.Map
Specified value type does not match the type expected for this container.
Don't know what else to try...I'm not sure cell vs. character array, vs cell array of characters, so perhaps that is the issue.....
I appreciate the help...
Jorge
댓글 수: 6
Walter Roberson
2025년 5월 7일
As of R2025a (currently pre-release), you will be able to use
dataStructArray = num2cell(dataValues,2);
to create a cell array in which each row is a cell array containing the variables.
Unfortunately in R2024b and earlier, building such a cell array is most easily done using a loop.
채택된 답변
Walter Roberson
2025년 5월 7일
이동: Walter Roberson
2025년 5월 7일
struct values are not permitted.
keys = 100:110;
values = struct('abc', num2cell(1:11))
containers.Map(keys, values)
댓글 수: 0
추가 답변 (2개)
Eric Sofen
2025년 6월 5일
Is there a reason you want to use a container other than table?
Are the SerialNumbers unique? If so, you can put them in the RowNames property of the table and "query" the table using subscripting:
TestTable_valid = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1833727/TestTable_valid.xlsx')
TestTable_valid.Properties.RowNames = TestTable_valid.SerialNumber;
TestTable_valid.SerialNumber = [];
TestTable_valid.Properties.DimensionNames(1) = "SerialNumber";
% get all data for one row
TestTable_valid("A020621208230001",:)
% get just PassFail for one row
TestTable_valid.PassFail("A020621208230001")
% and of course, table, unlike dictionary, means that if you need to slice
% across all SerialNumbers, you still can:
TestTable_valid.Model
댓글 수: 4
Eric Sofen
2025년 6월 9일
@Walter Roberson, Perhaps. That's why I asked the question. You and Matt are correct that if you just need to access data by looking up the SerialNumber, the fastest thing is to use a dictionary. (And we encourage you to use dictionary rather than containers.Map. Dictionary addresses several performance and behavioral issues with containers.). Lookup performance can be particularly important when doing a fast calculation in a tight loop and dictionary probably wins there. However, as I attempted to illustrate above, if Jorge wants to do things other than look up rows, then table may still be a good option. Vectorized calculations on one variable are a lot faster and easier on the table than the dictionary of structs.
Having real-world use cases is helpful for me as I think about table performance, because there are ultimately trade-offs in performance and functionality.
Walter Roberson
2025년 6월 9일
See by the way the timing tests I posted at https://www.mathworks.com/matlabcentral/answers/113258-is-there-any-concept-like-dictionary-or-hash-tables-in-matlab-like-in-python#comment_3254879
참고 항목
카테고리
Help Center 및 File Exchange에서 Structures에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!