How do i check for empty cells within a list

How do i check for empty cells within list? I have a list of cells, namelist, and it has 12 values, i need to check if some of the cells are empty. Thanks

 채택된 답변

the cyclist
the cyclist 2011년 9월 21일

41 개 추천

It is not clear what you mean by a "list of cells". Can you provide some code that illustrates what types of variables you are using? For example, by "cells", do you mean cell arrays?
Without more details, it is hard to answer your question, but there are functions isempty() and cellfun() that might be useful to you.
Edited in response to your comments:
>> cellfun(@isempty,namelist)
does what I think you want. For example:
namelist = cell(2,6)
namelist{1} = 'abc'
namelist{12} = 3
cellfun(@isempty,namelist)

댓글 수: 7

Andy
Andy 2011년 9월 21일
i just did
namelist = cell(2,6)
Andy
Andy 2011년 9월 21일
so now i am trying to check the different contents in the namelist thingy. so i want to do something like:
empty?((namelist(1,1))
and have the program return a 1 or 0, so i need to know what to put in place of empty?
Andy
Andy 2011년 9월 21일
is empty check for the whole list though, i just need to check individuals inside the list
Jan
Jan 2011년 9월 21일
cellfun('isempty') is much faster than cellfun(@isempty). The first version checks the number of elements directly in the Mex function, the later calls Matlab by mexCallMATLAB for each element.
This solution is faster and easier...
Jan
Jan 2017년 11월 24일
[EDITED, moved from flag to comment] LULU XUE wrote:
It's very useful!! inspiring! Thanks
WANG Jason
WANG Jason 2020년 4월 29일
This solution shows the power of Matrix:)

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

추가 답변 (2개)

topdawgnate
topdawgnate 2011년 9월 21일

9 개 추천

%Build Cell array (note the curly brackets)
A{1,1} = [1 4 3; 0 5 8; 7 2 9];
A{1,2} = 'Anne Smith';
A{2,1} = 3+7i;
A{2,2} = [];
Use the isempty function
isempty(A{2,2}) %Note the difference "{" and "(" brackets
This function will return 1.
Hope this helps. Nate

댓글 수: 2

Andy
Andy 2011년 9월 21일
exactly what i needed, thanks!
the cyclist
the cyclist 2011년 9월 21일
Be aware that this solution checks a single element within a cell array. My solution checks every element in the cell array individually, and reports whether each cell is empty or not. That might be more efficient, depending on your application.

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

Korosh Agha Mohammad Ghasemi
Korosh Agha Mohammad Ghasemi 2024년 6월 25일
이동: Voss 2024년 6월 25일

0 개 추천

To check for empty cells within a cell array in MATLAB, you can use the cellfun function combined with isempty. This approach is efficient and straightforward. Here’s how you can do it:
% Example cell array
namelist = {'John', [], 'Alice', 'Bob', [], 'Eve', 'Charlie', [], 'Dave', [], 'Frank', 'Grace'};
% Use cellfun with isempty to find empty cells
emptyCells = cellfun(@isempty, namelist);
% Display the indices of empty cells
emptyIndices = find(emptyCells);
disp('Empty cell indices:');
disp(emptyIndices);
This code will create a logical array emptyCells where each element is true if the corresponding cell in namelist is empty and false otherwise. The find function then returns the indices of the empty cells.
If you prefer a faster implementation, especially for large cell arrays, you can use the string form of cellfun:
emptyCells = cellfun('isempty', namelist);
emptyIndices = find(emptyCells);
disp('Empty cell indices:');
disp(emptyIndices);

카테고리

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

제품

태그

질문:

2011년 9월 21일

이동:

2024년 6월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by