cellfunc in tables alternative
1 view (last 30 days)
Show older comments
Hello,
I have a problem i had run this script with xlsread, but changed it to readtable(for convenience issues), now it seems that cellfunc don't work well with tables. I'm looking for an alternative to make this work
raw = readtable('Document.xls','Sheet', sheetName,'Range', 'A:C')
[data_a,datetime_str_a] = cleanup(raw)
%%
function [data,datetime_str] = cleanup(raw)
raw(cellfun(@(x) ~isempty(x) && isnumeric(x) && isnan(x),raw)) = {''};
%% Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells
%% Create output variable
I = cellfun(@(x) ischar(x), raw);
raw(I) = {NaN};
data = reshape([raw{:}],size(raw));
%% Remove empty data rows
Nrows = size(data,1);
for row = Nrows:-1:1
if all(isnan(data(row,:)))
data(row,:) = [];
else
break
end
end
%% Make date time string
% column 1 is UNIX timestamp (# seconds since 1-1-1970)
days = data(:,1)/86400; % # days since 1-1-1970
t0 = datenum('01.01.1970','dd.mm.yyyy'); % # days since 1-1-0000 (base for Matlab), plus 1 hr correction (timezone)
% t0 = datenum('01.01.1970','dd.mm.yyyy')+1/24; % # days since 1-1-0000 (base for Matlab), plus 1 hr correction (timezone)
daystot = t0+days;
datetime_str = datestr(daystot); % string (24-May-2017 08:08:02)
1 Comment
Walter Roberson
on 19 Aug 2019
Use detectImportOptions and setvartype() to force all of the variables to be numeric. The readtable() will then automatically convert any non-numeric entries to NaN.
... But probably you can just skip the cleanup phase. readtable() is pretty good at figuring out that a column is supposed to be numeric. It is not perfect, though, so if it is common for there to be entries that are remarks instead of numbers, then you might need to force it.
Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!