MATLAB Answers

0

Strange error using readtable

Asked by Eric Metzger on 8 Nov 2019
Latest activity Commented on by per isakson
on 9 Nov 2019 at 19:34
Greetings,
Having trouble with readtable with text files. The text files in question have a combination of text and number, thus the reason for using readtable. The script runs through the first two text files with no issues then crashes on the first line of the next text file.
while row <= height(workfile) %Height command identifies the number of rows in the table to stop the loop.
t=workfile(row, col); % t is a temp variable to store the date before processed by datenum
t=table2array(t); % convert to an array
t=datenum(t); % process to a datenum value
col=3; % set column to 3 to pull precip data
% now pull the precip data
p=workfile(row, col);
p=table2array(p);
Vdata=[t, p]; %temp variable to allow easier use of the fid process
fid=fopen (verfile , 'a');
fprintf(fid, '%6.0f, %7.4f \n ' , Vdata);
fclose(fid);
The datenum line executes fine but it appears the issue comes up with the fprintf command as I get this error
Error using fprintf
Function is not defined for 'cell' inputs.
Error in sitedata (line 95)
fprintf(fid, '%6.0f, %7.4f \n ' , Vdata);
LIke I said, this was not an issue with the previous text files that were sucessfully run before so I am stumped. Below is a sample of the text files in question:
10/31/2018 0 0
11/1/2018 0 0
11/2/2018 0 0
11/3/2018 0 0
11/4/2018 T 0.0001
11/5/2018 0 0
11/6/2018 0 0
11/7/2018 0 0
11/8/2018 T 0.0001
11/9/2018 0.19 0.19
11/10/2018 0 0
This is what the file before the one the crashed look like (top of the file).
Here is the one the script crashed on (same area of the file):
10/31/2018 0 0
11/1/2018 0.1 0.1
11/2/2018 0.45 0.45
11/3/2018 0 0
11/4/2018 0.5 0.5
11/5/2018 0.14 0.14
11/6/2018 0.11 0.11
11/7/2018 0 0
11/8/2018 0 0
11/9/2018 0.12 0.12
11/10/2018 0 0
Other than the data, they don't look any different. Any ideas??

  4 Comments

Show 1 older comment
Yes, the "T" entries work fine in the other files. Here is an example of a previous file that ran with a "T" value.
10/31/2018 0 0
11/1/2018 0 0
11/2/2018 0 0
11/3/2018 0 0
11/4/2018 T 0.0001
11/5/2018 0 0
11/6/2018 0 0
11/7/2018 0 0
11/8/2018 T 0.0001
11/9/2018 0.19 0.19
11/10/2018 0 0
11/11/2018 0 0
11/12/2018 0.2 0.2
11/13/2018 T 0.0001
I have looked over he file that consistently fails with this error. I can't find anything different about it. I have attached the full file as a reference.
In this script, the second colum is ignored as it was processed separatly by excel. Only columns 1 and 3 are used.
Here is the more of the code and the complete "while" loop for this part. Hopefully this explains it better.
D=dir('./*pre.txt'); % Identify the files to be processed
for i=1: length(D)
file=D(i,:) .name; % pull the file to be processed
workfile=readtable(file); % puts the file in a format that can be processed
% pull the date data out and reformat into a datenum
% variables for rows, columns and the lenght of the file
row=1;
col=1;
vfile=file(1:5); % pull the file name out for the complete verification file name
verfile=[vfile, 'ver.txt']; % make the output file name
station=file(1:3); % Identifies the verification site being worked with to set up the 72hr verification file
pfile=['Processing file: ' file];
disp(pfile)
precip72hrfile=[station, '72hrver.txt']; %72hr precipitation file
while row <= height(workfile) %Height command identifies the number of rows in the table to stop the loop.
t=workfile(row, col); % t is a temp variable to store the date before processed by datenum
t=table2array(t); % convert to an array
t=datenum(t); % process to a datenum value
col=3; % set column to 3 to pull precip data
% now pull the precip data
p=workfile(row, col);
p=table2array(p);
Vdata=[t, p]; %temp variable to allow easier use of the fid process
fid=fopen (verfile , 'a');
fprintf(fid, '%6.0f, %7.4f \n ' , Vdata);
fclose(fid);
row=row+1; %move to the next row
col=1; %reset the column to for the next time through the loop.
end
Hmmm...i see. I guess this is one that got by me. One of hte artifact errors the excel script was supose to identify and strip out. Will have to check.
To be sure, if there are letters and number like one Walter identified, will this make the "table2array" command fail?
If so,this could be why the table stayed a string and not conver to an array.
per isakson
on 9 Nov 2019 at 19:34
In brtyrpre.txt the pair
5/30/2019 S S
5/31/2019 0.22A 0.22A
appears twice. In your question
11/4/2018 T 0.0001
appears twice. Will other letters pop up?
The construct, which reads a value from the table
x = workfile( row, col );
x = table2array( x );
can be replaced by
x = workfile{ row, col };
"To be sure, if there are letters and numbers ..." See the two <3x3 table> in my answer and notice that one contains doubles the other characters. And read my text
With letters among the numerical data the entire column is imported as character, without letters it's imported as double. When "extracting" a character column from a table you get a cell array, which causes the error.
table2array doesn't fail!

Sign in to comment.

1 Answer

Answer by per isakson
on 8 Nov 2019
Edited by per isakson
on 8 Nov 2019
 Accepted Answer

I've downloaded brtyrpre.txt from your comments and copy&pasted brtyr_error_pre.txt from your question ("Here is the one the script crashed"). Next I run the script of your comment. brtyr_error_pre.txt was processed without problems and the script throw an the error for the first row of brtyrpre.txt.
>> Untitled
Processing file: brtyr_error_pre.txt
Processing file: brtyrpre.txt
Error using fprintf
Function is not defined for 'cell' inputs.
Error in Untitled (line 34)
fprintf( fid, '%6.0f, %7.4f \n ' , Vdata );
K>>
The the text of the error differs somewhat, but the message is the same. I run R2018b.
The reason for the error is that in one of the files there are letters among the numerical data (as Walter assumed). With letters among the numerical data the entire column is imported as character, without letters it's imported as double. When "extracting" a character column from a table you get a cell array, which causes the error.
"The script runs through the first two text files with no issues then crashes on the first line of the next text file." Run one file at a time to become 101% sure which file causes the error.
K>> workfile(1:3,:)
ans =
3×3 table
Var1 Var2 Var3
__________ ____ ____
10/31/2018 0 0
11/01/2018 0.1 0.1
11/02/2018 0.45 0.45
K>> workfile(1:3,:)
ans =
3×3 table
Var1 Var2 Var3
__________ ______ ______
10/31/2018 '0' '0'
11/01/2018 '0.1' '0.1'
11/02/2018 '0.45' '0.45'
And see

  2 Comments

In particular you have 0.25A and 0.22A
Note: there is a Suffixes option that can be set against the results of detectImportOptions to remove known suffixes, in the case where 0.25A is to be treated as if 0.25 were present instead.
Eric Metzger on 9 Nov 2019 at 12:40
Thanks to both of you (Walter and Per). I am not very proficent with MatLab but it is the code that is much easier for me to manipulate and get the results I want. I still have problems with little obviouse stuff like this.
Thanks again!

Sign in to comment.