Effacer les filtres
Effacer les filtres

Ignore comments in a csv file

21 vues (au cours des 30 derniers jours)
Aditya Mehrotra
Aditya Mehrotra le 11 Juil 2020
Commenté : Aditya Mehrotra le 14 Juil 2020
I have a .csv file that reads as the following:
# nodes.csv file for V-REP kilobot motion planning scene.
# All lines beginning with a # are treated as a comment and ignored.
# Each line below has the form
# ID x y heuristic-cost-to-go
# where ID is the unique integer ID number of the node (1 through N)
# (x y) is the location of the node in the plane and heuristic-cost-to-go
# is an optimistic estimate of the path length from that node to the
# goal node as needed by A* search.
1 -0.5 -0.5 1.4142
2 -0.09 -0.4 1.0762
3 -0.285 -0.305 1.1244
4 0.0575 -0.225 0.8494
5 -0.0525 -0.0175 0.7604
6 -0.37 0.3 0.8927
7 0.3525 -0.0525 0.5719
8 0.0625 0.255 0.5014
9 -0.1 0.3725 0.6134
10 0.4275 0.195 0.3135
11 0.345 0.3525 0.214
12 0.5 0.5 0
The file formatting is not fixed per se and can have multiple lines of comments/nodes when necessary. I realise the csvread() function does not ignore the comments. Can someone suggest a suitable snippet to ignore comments and extract numbers from the .csv file? I am fairly new to programming with Matlab and would appreaciate any help.
  2 commentaires
Walter Roberson
Walter Roberson le 11 Juil 2020
textscan with CommentStyle option
dpb
dpb le 11 Juil 2020
readtable and readmatrix also support 'CommentStyle' and are hgher-level than textscan

Connectez-vous pour commenter.

Réponse acceptée

Govind KP
Govind KP le 14 Juil 2020
You can use readmatrix function as follows:
>> nodes = readmatrix('nodes.csv', 'CommentStyle','#')
  1 commentaire
Aditya Mehrotra
Aditya Mehrotra le 14 Juil 2020
Thanks a lot! This works perfectly.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by