Effacer les filtres
Effacer les filtres

csvread not picking up the entire string?

2 vues (au cours des 30 derniers jours)
CAROL GARRITTY
CAROL GARRITTY le 28 Fév 2018
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?

Réponses (1)

Walter Roberson
Walter Roberson le 28 Fév 2018
Try readtable()
You have no hope with csvread() or dlmread(). It would be possible with textscan()
  1 commentaire
Walter Roberson
Walter Roberson le 1 Mar 2018
"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')

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by