Strange error using readtable
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
per isakson
le 9 Nov 2019
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!
Réponse acceptée
per isakson
le 8 Nov 2019
Modifié(e) : per isakson
le 8 Nov 2019
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 commentaires
Walter Roberson
le 8 Nov 2019
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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Cell Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!