Hello
I've got what in the screenshots below, so RawFileIndex is a table that contains Paramaters that is another table (or a 1x1 cell containing a table? I'm a bit confused...)
I want to know all the parameter ID accross all the files, something like unique(RawFileIndex.Parameters{:,1}.ID) but this is clearly not the right syntax... how do I do it?
thanks

 채택된 답변

Stephen23
Stephen23 2024년 8월 15일

0 개 추천

U = unique(vertcat(RawFileIndex.Parameters{:}).ID)

댓글 수: 5

Paolo Mazzoleni
Paolo Mazzoleni 2024년 8월 15일
thanks
Peter Perkins
Peter Perkins 2024년 8월 23일
This is a pretty advanced move. It's worth learning why it works, because there are several things going on that are useful to know:
  1. RawFileIndex is a table with 36 rows and a bunch of variables
  2. Its Parameters variable is a cell array. Tables can contain cell arrays
  3. Each of the 36 cells in RawFileIndex.Parameters contains a table, all of them with the same two variables, but with different numbers of rows. So that cell array allows you to encapsulate multiple rows of parameters for each row of RawFileIndex
  4. RawFileIndex.Parameters{:} expands those 36 cells into what's called a "comma-separated list", which is more or less a stream of 36 separate values, i..e. 36 tables
  5. vertcat(RawFileIndex.Parameters{:}) captures that stream and vertically concatenates the 36 tables. They all have the same variables, so that works. The result is one Nx36 table, where N is something like 36*33
  6. vertcat(RawFileIndex.Parameters{:}).ID extracts the ID variable of that Nx2 table. It's an Nx1 string column vector, because all of the ID variables in the 36 separate tables were string columns
  7. Finally, call unique on that string column
Paolo Mazzoleni
Paolo Mazzoleni 2024년 8월 29일
Thanks for the explanation, very useful.
I created and populated RawFileIndex myself earlier in the code, it's not something I imported from somewhere else, was it the right choice to use a table for it? whould you have structered it any differently?
Also, is it possible to have a table directly within another table or does it alway have to be a table in a cell within a table?
thanks
Stephen23
Stephen23 2024년 8월 29일
편집: Stephen23 2024년 8월 29일
"was it the right choice to use a table for it?"
If a table holds the information you require in a way that makes accessing it efficient and easy to understand for you, then it was a reasonable choice.
"whould you have structered it any differently?"
I probably would have used a structure array. In general use the simplest data structure that can reasonably contain the data.
"is it possible to have a table directly within another table or does it alway have to be a table in a cell within a table?"
Tables must be in a cell array.
Every table column actually consists of one array, which therefore must have exactly as many rows as the table has. However, in general tables have arbitrary number of rows, so in general the number of rows of nested tables would not sum to the number of rows of the parent table. Therefore each nested table is instead placed in a cell array (which does have exactly the same number of rows as the parent table). Exactly the same argument applies to all other arrays where non-scalar rows must be stored in one table row.
Paolo Mazzoleni
Paolo Mazzoleni 2024년 8월 29일
thanks

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Tables에 대해 자세히 알아보기

제품

릴리스

R2021b

태그

질문:

2024년 8월 15일

댓글:

2024년 8월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by