フィルターのクリア

How do I find the number of "struct" class in a structure array?

17 ビュー (過去 30 日間)
Ferd
Ferd 2019 年 4 月 7 日
コメント済み: Ferd 2019 年 4 月 10 日
Hi,
Is there a way to determine the number of "struct" names in the structure array?
This way I could elimate the previous non-struct variables created.
Thanks
Ferd
  1 件のコメント
Clay Swackhamer
Clay Swackhamer 2019 年 4 月 8 日
Hi Ferd maybe this will help. Let's say we defined a structure to hold a matrix, a cell, and a function. Could look like this:
s = struct %declare structure
s.someNumbers = [1, 2, 3] %add a matrix to the structure
s.someText = {'hello', 'world'} %add some text to the structure
s.someFunction = @(x) 2*x.^2 %add a function to the structure
Now, the function holds three different things. To get the names of these fields we can use
fieldNames = fieldnames(s)
To get the number of fields in the structure use
numFields = length(fieldNames)

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

採用された回答

Walter Roberson
Walter Roberson 2019 年 4 月 8 日
nnz(cellfun(@isstruct, struct2cell(YourStruct)))
  1 件のコメント
Ferd
Ferd 2019 年 4 月 10 日
Exactly what was needed!
Thanks!

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

その他の回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by