Find index of first empty cell in an array row

18 ビュー (過去 30 日間)
Ahmad Mahmoud
Ahmad Mahmoud 2018 年 7 月 3 日
編集済み: Jan 2020 年 12 月 1 日
I have a cell with strings and matrices. I want to add data after the last cell with data in a row.
A = {['apple'] [3x2 double] ; ['banana'] []}
For example, I want to add a matrix after the 3x2 matrix in first row, or after the 'banana' in second row.
I tried using:
find(cellfun(@isempty,A{1,:}),1)
But I get the error:
Error using cellfun
Input #3 expected to be a cell array, was double instead.
  1 件のコメント
Stephen23
Stephen23 2018 年 7 月 3 日
編集済み: Stephen23 2018 年 7 月 4 日
The syntax A{1,:} places the contents of the cell array into a comma separated list, so what you wrote is equivalent to writing this:
cellfun(@isempty,'apple','banana',[3x2 double],[])
and none of those inputs are cell arrays. See also:

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

採用された回答

Hermes Suen
Hermes Suen 2018 年 7 月 3 日
The error you are receiving seems to be because you are using curly braces after A.
A{1,:}
When you use curly braces with a cell array, you access the actual contents of what is within the cell array. So if you have a cell array of char vectors and doubles, for example:
A = {'Banana', 560}
A{1}
A{1} will yield only the char vector 'Banana'. A{2} will yield a double, 560. But the cell argument into cellfun, must be an actual Cell array, not the contents of that cell array. I suggest using regular parenthesis to index the cell array A.
A(1)
This will yield a another Cell array of length 1 that is a subset of A.
  1 件のコメント
Ahmad Mahmoud
Ahmad Mahmoud 2018 年 7 月 4 日
Thanks! So simple.

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

その他の回答 (1 件)

Jan
Jan 2018 年 7 月 4 日
編集済み: Jan 2020 年 12 月 1 日
A = {'apple', rand(2, 3); 'banana', []};
find(cellfun('isempty', A),1)
Note: Using the cellfun method 'isempty' is faster that providing a function handles @isempty.
There is no need to enclose a CHAR in square brackets. This wastes time only.
  1 件のコメント
Peeyush Awasthi
Peeyush Awasthi 2020 年 12 月 1 日
Hi Jan, Thanks that was the best answer even using unique.

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by