Error message: First input can not be empty. Expected a non-empty character vector or a valid file-id.

18 visualizaciones (últimos 30 días)
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 comentarios
Stephen23
Stephen23 el 8 de En. de 2020
Editada: Stephen23 el 8 de En. de 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.
Kira Prouse
Kira Prouse el 8 de En. de 2020
You were right about there being an extra empty line at the end, it has now loaded. With regards to the other part of your comment. I am basically inept at MATLAB and understanding it, in class we ran through some scenarios using the code you see above. I don't know enough about it to know what is imperitive to the task at hand or not.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Large Files and Big Data en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by