csvread not picking up the entire string?
3 次查看(过去 30 天)
显示 更早的评论
I have a CSV text file containing the following comma separated values (date,time,real1,real2):
2018-02-28,20:21:26.000,33.345,-22.123
When I run the script
filename = '2018-02-28_receiver_1.csv';
M = csvread(filename)
M reports back with
2018 -2 -28 20
I'd like M to pick up the complete string between the commas, like Excel does.
I'd like M to read
2018-02-28 20:21:26.000 33.345 -22.123
I tried xlsread.
That generates an error.
Unable to read XLS file /home/user/2018-02-28_receiver_1.csv. File is not in recognized format.
Now if I try importdata I get a 1x1 struct,
O =
struct with fields:
data: [38.8927 -77.4406]
textdata: {'2018-02-28' '20:21:26.000'}
colheaders: {'2018-02-28' '20:21:26.000'}
That's not what I expected. I guess I could fiddle with extracting the data from the struct.
Is there a more straightforward way to read a CSV file?
I also tried:
T = readtable(filename,'ReadVariableNames',false,...
'Format','%{yyyy-mm-dd}D %{HH:MM:SS}D %f %f')
That got me a bunch of formatting errors.
I then tried:
T = readtable(filename,'ReadVariableNames',false,...
'Format','%q %q %f %f')
That got a little closer...
I guess if I read the documentation I would find the proper format. This finally works.
There are some fine nuances between HH:mm:ss and HH:MM:SS.
filename = 'receiver_2.csv';
T = readtable(filename,'ReadVariableNames',false,...
'Format','%{yyyy-mm-dd}D %{HH:mm:ss}D %f %f')
Going back to csvread, does csvread work with a format string?
回答(1 个)
Walter Roberson
2018-2-28
Try readtable()
You have no hope with csvread() or dlmread(). It would be possible with textscan()
1 个评论
Walter Roberson
2018-3-1
"Going back to csvread, does csvread work with a format string?"
No, it does not. You have no hope of getting csvread() or dlmread() to work for this.
T = readtable(filename,'ReadVariableNames',false,...
'Format','%{yyyy-mm-dd}D %{HH:mm:ss}D %f %f')
should be
T = readtable(filename,'ReadVariableNames',false,...
'Format','%{yyyy-MM-dd}D %{HH:mm:ss}D %f %f')
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!