Error message: First input can not be empty. Expected a non-empty character vector or a valid file-id.
18 views (last 30 days)
Show older comments
I had no problem loading in other years datas i.e 2012 and 2011, the .txt files are in the same format but I get the error message :
Error using textscan
First input can not be empty. Expected a non-empty character vector or a valid file-id.
Error in load_tide_data (line 19)
tmp=textscan(tmpl,'%s %s %s %s %s')
when trying to load 2018 data.
function data2018=load_tide_data(filename)
% The fun%function to load tide data from BODC
% input i%input is filename of the data file
% output is the date/time and height from the data file
data2018.filenamet=filename;
fid=fopen(filename);
for I=1:12
tmpl=fgetl(fid);
end
J=1;
while ischar(tmpl)
tmp=textscan(tmpl,'%s %s %s %s %s')
data.t(J)=datenum([ cell2mat(tmp{2}) ' ' cell2mat(tmp{3}) ]);
tmph=str2num(cell2mat(tmp{4}));
if isempty(tmph)
tmph=NaN;
end
data.h(J)=tmph;
tmpl=fgetl(fid);
J=J+1;
end
fclose(fid);
I don't understand why this year won't load. Any help would be much appreciated!
2 Comments
Stephen23
on 8 Jan 2020
Edited: Stephen23
on 8 Jan 2020
tmpl is probably an empty character array or numeric array. Possibly this happens at the end of a file (e.g. a trailing newline) or an empty line in the middle of the file: your code is not robustly written to handle such cases.
Is there a particular reason why most of your function allocates data to the structure data which you then just discard entirely (exactly as the comments to your earlier question also pointed out)?
Based on this example and also your last question, you appear to be under the incorrect impression that you need to edit/change the internal variable name data2018 for each different file.
Answers (0)
See Also
Categories
Find more on Large Files and Big Data 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!