How to make readtable always return NaN when it detects a blank cell
Show older comments
My matlab code always generate NaN for blank cells. When I run it now I sometimes get NaN and sometimes I get {0×0 char}. I am reading in an excel file that for both examples attached is generated by the same R-code.
clear all;
close all;
%Test 1 Does work
fn = 'IMPORT_CONTROL.xlsx';
tBC = [];
opt = detectImportOptions(fn);
shts = sheetnames(fn);
new_details = cell(length(shts),2);
new_details{1,1} = [shts(1)];
readtable(fn,opt,'Sheet',shts(1));
new_details{1,2} = [tBC;readtable(fn,opt,'Sheet',shts(1))];
new_details{1,2}(:,6)
% Test 2
fn = 'IMPORT_Network5.xlsx';
tBC = [];
opt = detectImportOptions(fn);
shts = sheetnames(fn);
new_details = cell(length(shts),2);
new_details{1,1} = [shts(1)];
readtable(fn,opt,'Sheet',shts(1));
new_details{1,2} = [tBC;readtable(fn,opt,'Sheet',shts(1))];
new_details{1,2}(:,6)
1 Comment
Set the variable type to numeric:
Answers (1)
It seems that columns that contain numbers are interpreted as numeric, and missing data are replaced by NaN, whereas empty columns or columns with character entries are interpreted as char, and missing data are replaced by empty cells.
1 Comment
Walter Roberson
on 21 Sep 2024
If I recall correctly, a completely empty column is normally treated as text.
You can detectImportOptions and set the appropriate variable types to numeric.
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!