MATLAB Answers

How to determine if a cell has numeric data?

115 ビュー (過去 30 日間)
Zachary Reyes
Zachary Reyes 2018 年 10 月 25 日
回答済み: Giovanni Liverani 2019 年 11 月 22 日
How would I go about making a function that determines if a cell contains numeric data? It is a single cell, not a cell array. I am trying to determine if the data is numeric and the dimensions of the numeric array. If the data is not numeric, the dimensions are an empty set. Here is what I have so far:
function [isNum, dim] = isCellNumeric(c)
if (isnumeric(c))
isNum = true;
dim = size(c);
else
isNum = false;
dim = {};
end
end
Any help is appreciated, thanks!

  2 件のコメント

Guillaume
Guillaume 2018 年 10 月 25 日
Your function has no documentation. In particular, it does not say what type of variable it is expecting c to be.
If your code is supposed to say if a scalar cell array contains a numeric array then your code of course doesn't do that since it never looks at the content of the c container. It only looks at the type of the container itself.
Note that a single cell, not a cell array is a bit meaningless. a cell array with just one cell is still a cell array.
In my opinion, it would be more consistent to return an empty matrix rather than an empty cell array if the content is not numeric (since you return a matrix when it is). So I would have dim=[] in the else.
Zachary Reyes
Zachary Reyes 2018 年 10 月 25 日
The empty matrix makes sense; I will fix the code to look at the contents, thanks a lot.

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

回答 (2 件)

OCDER
OCDER 2018 年 10 月 25 日
% checkCellForNum will look at each element of a cell array to look for numeric
% elements. Returns a logical array of 1 for numeric positions and a cell array
% of dimensions for only the numeric cell elements.
%
% EXAMPLE
% C = {[1 3] 2 'b' 'c'};
% [IsNum, Dim] = checkCellForNum(C)
function [IsNum, Dim] = checkCellForNum(C)
IsNum = cellfun('isclass', C, 'double'); %Only checks for double. If you want the same output as isnumeric, use cellfun(@isnumeric, C)
Dim = cell(size(C));
Dim(IsNum) = cellfun(@size, C(IsNum), 'un', 0);

  0 件のコメント

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


Giovanni Liverani
Giovanni Liverani 2019 年 11 月 22 日
The way is solved it is:
waitfor(msgbox('You are about to participate in a "... Task". Before we continue, you will be asked to insert your personal details (student ID, gender, age) below.'));
prompt = {'Enter Student ID:','Gender (1: Male, 0:Female):', 'Age'};
dlgtitle = 'Input';
dimension = [1 45];
definput = {'123456789','1','21'};
SubjectData = inputdlg(prompt,dlgtitle,dimension,definput);
waitfor(SubjectData);
if isnan(str2double(SubjectData{1})) == 1 || isnan(str2double(SubjectData{2})) == 1|| isnan(str2double(SubjectData{3})) == 1
waitfor(errordlg('Invalid Value. Call assistant to restart the experiment', 'Error'));
return
end

  0 件のコメント

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

タグ

タグが未入力です。

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by