Strange error using readtable
显示 更早的评论
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 个评论
Walter Roberson
2019-11-8
Could you confirm that the files that work properly have 'T' entries in some rows, and that the files that do not work do not have 'T' entries? I would have expected the other way around.
Or perhaps there are text entries in the part you did not show us in column 3 of the file that does not work but no text entries in column 3 of the ones that work?
If there are text entries similar to the 'T' we can see in the example then what do you want done with them?
Eric Metzger
2019-11-8
编辑:per isakson
2019-11-8
Eric Metzger
2019-11-9
per isakson
2019-11-9
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!
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Text Data Preparation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!