Check if all data in a table are numbers

52 ビュー (過去 30 日間)
Hannes Truter
Hannes Truter 2019 年 10 月 8 日
コメント済み: Hannes Truter 2019 年 10 月 9 日
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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by