Check if all data in a table are numbers

31 次查看(过去 30 天)
I need to check that all columns of a data table I receive from an user's Excel file contain only numeric values and no text before I send it to a plotting function. I have created a function that works but I'm convinced it can be done in a more efficient way than using the for loop that I currently have.
The function receives a data table and the amount of columns from another function that reads the table from an Excel file with the following code
DataTable = readtable(filename,'Sheet',SheetName);
The working code for my validation check is below:
function [ValidData] = ValidHistogramData(DataTable,DColumns)
%Check one column at a time to see if it is of type numeric
for DCCount = 1:DColumns
%Only need to check if 1st row of table is numeric since all rows below have the same type
if ~isa(DataTable{1,DCCount},'numeric')
ValidData = false;
%If any column is not numeric there is no need to check further
return
else
ValidData = true;
end
end
end
I'm fairly new to MATLAB and still don't fully understand the matching indexing methods to check if something is present in a table. I suspect this method will be much more efficient than a for loop. Below is one of my many unssucessful attempts just to show what I mean with matching indexing methods.
ValidRows = DataTable(isa(DataTable{:,6},'numeric'),:);
Most of my attempts give me the error "Cannot concatenate the table variables 'Time' and 'VIN', because their types are double
and cell."
%##################################################################
% PLEASE IGNORE EVRYTHING FROM MY ORIGINAL POST BELOW
% I'm editing this after the first answer was provided.
% After further testing I have confirmed this code does not work
%##################################################################
Through trial and error I came up with the following code that looks like it works but I have no faith that it does what I want. I suspect it just accidentally works with the different tables I tested on it. I would please like to get confirmation if the code below is actually checking that all columns of the table are numeric.
ValidData = isa(DataTable(1,:),'numeric');

采纳的回答

Joe Vinciguerra
Joe Vinciguerra 2019-10-8
This is how I would approach it...
DataTable = readtable(filename,'Sheet','Sheet1'); % load your data
TF = cellfun(@isnumeric,table2cell(DataTable(1,:))); % look at the first row of the table, convert to a cell array temporarily, then create a logical array based on isnumeric
if (numel(TF(TF == false)) > 0) % if any elements failed and set your flag
ValidData = false;
else
ValidData = true;
end
Line 2 is doing a couple things: since I'm using cellfun to evaluate the condition isnumeric then the table needs to be converted to a cell array. I'm also specifying to only look at the first row. You can look at different rows or columns by changing (1,:) to something else.
Line 3: TF(TF==0) says look at array TF and pull the elements of where TF equals false. numel() counts how many elements that if found matching that criteria. So if there are more than 0 set your flag.
  1 个评论
Hannes Truter
Hannes Truter 2019-10-9
Thank you very much. This is definitely a lot more efficient than my code.
Thank you for also clearly explaining how the different sections work. You have shown me several features of MATLAB I was unaware of.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by