Know the number of row within a structure (struct)

47 ビュー (過去 30 日間)
Alberto Acri
Alberto Acri 2022 年 11 月 10 日
コメント済み: Alberto Acri 2022 年 11 月 10 日
Hi! I have the following 'CODE' struct.
I would like to know if there is a code to determine the row number (present in 'Fields', for example 7) considering this as input:
folder = 'C:\Users\Alberto\Desktop\GLOBAL\DATA 4'
Thanks!
  2 件のコメント
Stephen23
Stephen23 2022 年 11 月 10 日
編集済み: Stephen23 2022 年 11 月 10 日
"(present in 'Fields', for example 7)"
The heading is misleading: the column headers are the fieldnames (e.g. "name", etc), the row numbers are indices of elements of the structure array (you can see there are 12 row numbers, exactly matching the number of elements in that 12x1 structure).
Note that "DATA 4" is listed in elements 7, 8, and 9 of that structure array.
Alberto Acri
Alberto Acri 2022 年 11 月 10 日
編集済み: Alberto Acri 2022 年 11 月 10 日
@Stephen23 Yes, I know! I wanted to determine element 7 of struct CODE in correspondence of 'name' 'a'.
But the same is true for example at element 1,4,10 of struct CODE at 'name' 'a' where 'DATE 1', 'DATE 11' and 'DATE 6' are given respectively.

サインインしてコメントする。

採用された回答

Joseph Cheng
Joseph Cheng 2022 年 11 月 10 日
so you can use the function find() and contains(). in this example i am just searching the name entry but it should work with your folder instead.
abc = 'a'-1;
for ind = 1:10
CODE(ind).name = char(abc+ind);
end
foundRow = find(contains({CODE.name},'d'))
foundRow = 4
Where by doing {CODE.name} here it'll make each entry into a cell then find which one contains your desired text.
  4 件のコメント
Stephen23
Stephen23 2022 年 11 月 10 日
編集済み: Stephen23 2022 年 11 月 10 日
@Alberto Acri: note that Joseph Cheng just used the loop to create some fake data to demonstrate how the code works. Of course you will not use the loop and you will use your own data. Try this to match the names:
foundRows = find(matches({CODE.name},'d'))
or if you want to match the folders (as your question specifies):
folder = 'C:\Users\Alberto\Desktop\GLOBAL\DATA 4'
foundRows = find(matches({CODE.folder},folder))
Alberto Acri
Alberto Acri 2022 年 11 月 10 日
perfect @Stephen23!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by