advice on table storage in a struct or a mat file
Show older comments
Hi, I have a large script which produces a very useful results-table summary of the part I am testing. As the test is standardised it would be useful to have a way to store all of the tables. It is likely that one of the parts may get tested multiple times and I still need to have a results-table stored for each instance of its having been tested. Then I would like to access these results-tables and be able to plot a value from them, maybe one value(element) from each of the results-tables so I can plot how that parameter has varied over a year etc.
Will this be easier to achieve by using a table/cell array within a mat file or with a structure? I have little experience with both of these so it makes sense to seek some general advice first.
%Example of a results table for Part001 (reduced to 2 columns of results)
%mask an array of dummy data;
A=num2cell(ones(10,3));
A(:,1)={'H1';'H2'; 'H3';'H4';'H5';'H6';'H7';'H8';'H9';'H10'};
A(:,2)={6.6;4.5;3.3;8.5;8;6;7;7.2;7.7;6.9};
A(:,3)={1.36;0.35;2.003;1.005;0.88;0.201;1.118;2.011;0.613;0.012}
%convert the array into a dummy table
vars={'PartID','Length','Variation'}
B=cell2table(A)
B.Properties.VariableNames = {'PartID','Length','Variation'}
I would have a table like this for many parts, the amount of these tables will increase as the year(s) go on. It is likely I would be interested in plotting two things, comparing Part001 with say Part087 thus wanting to see the values of both on the same axes. Also to only select an element of Part001, say the length and variation of 'H6' for each result table I have, and plotting all of those in a single graph to show the trend over a year etc.
I'm not sure if this will be easier in the long run to use datasets, structs or a matfile to do this, or maybe a 3D table. Let me know if this makes sense, happy to explain further.
2 Comments
Peter Perkins
on 9 May 2017
Stephen, I think you're gonna need to provide a short, clear, and specific example.
Stephen Devlin
on 11 May 2017
Accepted Answer
More Answers (1)
Peter Perkins
on 12 May 2017
First thing is probably to get rid of all those cell arrays:
PartID = {'H1';'H2'; 'H3';'H4';'H5';'H6';'H7';'H8';'H9';'H10'};
Length = [6.6;4.5;3.3;8.5;8;6;7;7.2;7.7;6.9];
Variation = [1.36;0.35;2.003;1.005;0.88;0.201;1.118;2.011;0.613;0.012];
B = table(PartID,Length,Variation)
It would be easier to answer your question with a more specific example of what you want to do. But I guess it's sort of chicken and egg, if you could write code to show what you want to do, you might not have to ask the question.
I'm not clear on whether each "part" has one and only one of these tables, or if the same part is tested multiple times. Also not sure if the example table is typical of the actual size. So to a certain extent, I'm just guessing.
You almost certainly do not want a flat structure array. You could create one table for all parts, using an indicator value to sow what part each row corresponds to. If one part is tested multiple times, you'd need some kind of time stamp too. That kind of flat layout allows to to do any comparison or selection you want -- one part vs. another, all H1's across all parts, only result with Length > 6, and so on. It's at the expense of some storage inefficiency (storing 'Part001' 10 times) and at the expense of ease of access for one part's results (you do something like B(B.Part=='Part001',:)).
An alternative might be a table with one row for each part, with maybe a timestamp for the test and some other data that are constant for that part, and then one variable that is a cell array, each cell of which contains a table just like you example. That makes it really easy to get one part's data (using row names would make it something like B.Results('Part001')) but much harder to compare across all parts. If you didn't have any "constant" data, a scalar struct with each field named like B.Part001 containing a table, would be essentially equivalent.
Hope this helps.
1 Comment
Stephen Devlin
on 15 May 2017
Edited: Stephen Devlin
on 15 May 2017
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!