How can I query whether a cell is full?

1 view (last 30 days)
Berfin Çetinkaya
Berfin Çetinkaya on 2 Apr 2022
Answered: Voss on 2 Apr 2022
Hi
How can I query whether a cell is full? For example, I want to do something like this using the if structure: if this cell is full, do the action, if not, don't.
thank you for help
  8 Comments
KALYAN ACHARJYA
KALYAN ACHARJYA on 2 Apr 2022
Edited: KALYAN ACHARJYA on 2 Apr 2022
If A as an Normal array having size rows=2 & column=2
A=[3 4;8 5]
A = 2×2
3 4 8 5
Here A(3,2) having no sense, as index exceeds matrix dimension. As in matrix there is no empty elements, either it should be numbers or NaN, but it must be.
Hence calling any matrix elements which is out of Matrix dimension have no sense.
On the other hand in cell array which is possible
Lets suppose A is cell array having sizes 2x2
A={2,3;[] 'Kalyan'}
A = 2×2 cell array
{[ 2]} {[ 3]} {0×0 double} {'Kalyan'}
Where
A{2,1} is an empty and other having cell ememnts ('whch you may referring as "Full")
Hope it Helps! :)

Sign in to comment.

Answers (1)

Voss
Voss on 2 Apr 2022
Based on the comments it sounds like you need to use size to check whether the given row and column indices are valid for the given matrix.
A = [3 4; 8 5];
[m,n] = size(A);
rows_to_test = [2 3];
cols_to_test = [2 2];
for kk = 1:numel(rows_to_test)
ii = rows_to_test(kk);
jj = cols_to_test(kk);
% check that indices ii and jj are both valid:
if ii >= 1 && ii <= m && jj >= 1 && jj <= n
result = A(ii,jj);
fprintf('A(%d,%d) = %f',ii,jj,result);
else
result = [];
fprintf('A(%d,%d) does not exist: size(A) = [%d %d]',ii,jj,m,n);
end
end
A(2,2) = 5.000000
A(3,2) does not exist: size(A) = [2 2]

Tags

Community Treasure Hunt

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

Start Hunting!