Check if all data in a table are numbers
Ältere Kommentare anzeigen
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');
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Logical finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!