Why do I have an Error using readtable?

35 views (last 30 days)
Hannah
Hannah on 16 Aug 2021
Commented: Hannah on 17 Aug 2021
Does anyone know why I get the following error when running my code (shown below):
Reading failed at line 26. All lines of a text file must have the same
number of delimiters. Line 26 has 0 delimiters, while preceding lines
have 4.
Note: readtable detected the following parameters:
'Delimiter', '\t ', 'MultipleDelimsAsOne', true, 'ReadVariableNames',
false, 'Format', '%q%f%f%f%f'
Error in EXP2_Ver11_Evaluation_of_Meteonorm_simulation (line 68)
T = readtable(File_from_Meteo2, 'Headerlines', 12); %table
starts at line 12
The error mentions "Line 26" but I only ask it to read upto line 12 maximum.
I have also attached one of the files I am reading.
months=1:12;
for m=1:length(months)
tempFileName2 = sprintf('t%d_o%d_zero-mon.txt',t,o);%get the zero file
File_from_Meteo2 = append(pathResults, tempFileName2); %Combine path and file name then assign it to the variable 'File_from_Meteo' to access the file
T = readtable(File_from_Meteo2, 'Headerlines', 12); %table starts at line 12
%If the txt file is for t=1, (i.e. tilt of 0) it represents a situation with no tilt, therefore read colum "H_Ghhor"
if t==1
zero_subset = T(m:m, 'H_Gh');
%Otherewise the txt file represents a situation with a tilt, therefore read colum "H_Gkhor"
else
zero_subset = T(m:m,'H_Gk');
end

Accepted Answer

Dave B
Dave B on 16 Aug 2021
Edited: Dave B on 16 Aug 2021
I don't see where you sepcified the last row you wanted to read, you specified to ignore the first 12. Range is good for this kind of thing:
t=readtable('t1_o1_zero-mon.txt','Delimiter','\t','Range','13:24')
t = 12×5 table
Var1 Var2 Var3 Var4 Var5 _______ ____ ____ ____ ____ {'Jan'} 33 17 55 0.5 {'Feb'} 52 29 60 1.4 {'Mar'} 97 51 97 5.6 {'Apr'} 123 60 113 9.7 {'May'} 161 91 115 13.7 {'Jun'} 178 79 160 18 {'Jul'} 176 76 165 19.3 {'Aug'} 153 67 148 18.7 {'Sep'} 105 52 102 14.5 {'Oct'} 63 42 51 10.2 {'Nov'} 33 18 49 4.9 {'Dec'} 24 16 32 1.2
As a bonus, you can also read the variable names directly:
t=readtable('t1_o1_zero-mon.txt', 'Delimiter', '\t', 'Range', '11:24', 'ReadVariableNames', true)
t = 12×5 table
Month H_Gh H_Dh H_Bn Ta _______ ____ ____ ____ ____ {'Jan'} 33 17 55 0.5 {'Feb'} 52 29 60 1.4 {'Mar'} 97 51 97 5.6 {'Apr'} 123 60 113 9.7 {'May'} 161 91 115 13.7 {'Jun'} 178 79 160 18 {'Jul'} 176 76 165 19.3 {'Aug'} 153 67 148 18.7 {'Sep'} 105 52 102 14.5 {'Oct'} 63 42 51 10.2 {'Nov'} 33 18 49 4.9 {'Dec'} 24 16 32 1.2
  6 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!