xlsread import empty cell from excel file

조회 수: 12 (최근 30일)
wessel ter Laare
wessel ter Laare 2020년 8월 20일
댓글: wessel ter Laare 2020년 8월 21일
Good day,
I have created a script where I run through various excel files stored in a folder, and import certain areas into Matlab. As per below short example:
Test_files = dir([SourceFolder '*.xlsx']);
No_of_Tests = size(Test_files,1);
for n = 1:No_of_Tests
Test_filename = Test_files(n).name;
X(n,:) = xlsread(Perf_filename,'TEST','D101'); %Parameter 1
end
In some instances the field in the excel file is left blank. At that time my script crashed.
How can I make xlsread import an empty cell as NaN or Blank or something similar?
Thanks in advance for your kind assistance.
Rgrds,

채택된 답변

dpb
dpb 2020년 8월 20일
편집: dpb 2020년 8월 20일
Can't that way... xlsread imports what it finds as numeric and an empty cell is not numeric. You could use the multiple optional returns and read the raw cell that (I think, untested) will return an empty cell.
Probably the easiest kludge to fix your problem is
Test_files = dir([SourceFolder '*.xlsx']);
No_of_Tests = size(Test_files,1);
for n = 1:No_of_Tests
try
X(n,:) = xlsread(Test_files(n).name,'TEST','D101');
catch
X(n)=nan;
end
end
But, per the doc xlsread is deprecated, anyways; use one of the suggested alternates (altho even there you may have trouble with a single empty cell expecting numeric result).
  댓글 수: 1
wessel ter Laare
wessel ter Laare 2020년 8월 21일
Hi dpb,
Thanks for your reply, much appreciated.
You are correct, when using the raw option it does work. Only for some reason import a 1 x 2 cell with first value [] and second value as NaN.
[~,~,X(n,:)]
Think I can work with this.
Rgrds,

댓글을 달려면 로그인하십시오.

추가 답변 (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