필터 지우기
필터 지우기

csvread not picking up the entire string?

조회 수: 2 (최근 30일)
CAROL GARRITTY
CAROL GARRITTY 2018년 2월 28일
댓글: Walter Roberson 2018년 3월 1일
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
Walter Roberson 2018년 2월 28일
Try readtable()
You have no hope with csvread() or dlmread(). It would be possible with textscan()
  댓글 수: 1
Walter Roberson
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 CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by