MATLAB How to extract data from a CSV file with MATLAB?

  • Thread starter Thread starter Erikve
  • Start date Start date
AI Thread Summary
The discussion centers around reading data from a CSV file in MATLAB, specifically attempting to skip the first nine lines of the file. The original attempt used the `textread` function with a comma delimiter, which was incorrect since the data is actually semicolon-delimited. The correct approach involves modifying the `textread` command to use a semicolon as the delimiter and specifying the number of header lines to skip. A suggested solution was provided, which successfully reads the data but results in a cell array format that complicates accessing the data directly. The user is seeking further assistance on how to extract the contents from the resulting structure, as simply indexing does not yield the expected data. The conversation highlights the importance of correctly identifying file delimiters and understanding data structures in MATLAB.
Erikve
Messages
17
Reaction score
0
I'm sure that this will be a quite simple question, but I will not work for me. I try to read data from a csv-file with matlab(see under post). But is doesn't work... I try to skip the the first 9 ninen lines, because they are useless for the data analyses. I try the following line with matlab:
[data] = textread('data.csv','','delimiter',',','emptyvalue',NaN)

Can somebody help me to change this line? Thanks for every help!

greetings,
Erik

0;63;1;63;500;1000;0;4
0;1;1;150;1000;1500
0;2;2;150;1000;1500
0;3;3;150;1000;1500
0;4;4;150;1000;1500
0;5;5;150;1000;1000
0;6;6;150;1000;1000
0;7;7;150;1000;1000
0;8;8;150;1000;1000
1; 2; 792;2314; Led;-255; 3; 2; 1500; 7; 1; NaN; NaN
2; 2; 892; 892; Led;-255; 6; 1; 1500; 7; 1; NaN; NaN
3; 2; 835; 835; Led;-255; 9; 1; 1500; 7; 1; NaN; NaN
4; 2; 881; 881; Led;-255; 12; 1; 1500; 7; 1; NaN; NaN
5; 2; 560; 560; Led;-255; 15; 1; 1500; 7; 1; NaN; NaN
6; 2; 507; 507; Led;-255; 18; 1; 1500; 7; 1; NaN; NaN
7; 2; 652; 652; Led;-255; 21; 1; 1500; 7; 1; NaN; NaN
8; 2; 704; 704; Led;-255; 24; 1; 1500; 7; 1; NaN; NaN
9; 2; 502; 502; Led;-255; 27; 1; 1500; 7; 1; NaN; NaN
10; 2; 738;1495; Led;-270; 3; 2; 1500; 7; 1; NaN; NaN
11; 2; 901; 901; Led;-270; 6; 1; 1500; 7; 1; NaN; NaN
12; 2; 767; 767; Led;-270; 9; 1; 1500; 7; 1; NaN; NaN
13; 2; 940; 940; Led;-270; 12; 1; 1500; 7; 1; NaN; NaN
14; 2; 852; 852; Led;-270; 15; 1; 1500; 7; 1; NaN; NaN
15; 2; 848; 848; Led;-270; 18; 1; 1500; 7; 1; NaN; NaN
16; 2; 853; 853; Led;-270; 21; 1; 1500; 7; 1; NaN; NaN
17; 2; 546; 546; Led;-270; 24; 1; 1500; 7; 1; NaN; NaN
18; 2; 521; 521; Led;-270; 27; 1; 1500; 7; 1; NaN; NaN
19; 2; 655;1494; Led;-285; 3; 2; 1500; 7; 1; NaN; NaN
20; 2; 661; 661; Led;-285; 6; 1; 1500; 7; 1; NaN; NaN
21; 2; 901; 901; Led;-285; 9; 1; 1500; 7; 1; NaN; NaN
22; 2; 707; 707; Led;-285; 12; 1; 1500; 7; 1; NaN; NaN
23; 2; 569; 569; Led;-285; 15; 1; 1500; 7; 1; NaN; NaN
24; 2; 819; 819; Led;-285; 18; 1; 1500; 7; 1; NaN; NaN
25; 2; 777; 777; Led;-285; 21; 1; 1500; 7; 1; NaN; NaN
26; 2; 886; 886; Led;-285; 24; 1; 1500; 7; 1; NaN; NaN
27; 2; 544; 544; Led;-285; 27; 1; 1500; 7; 1; NaN; NaN
28; 2; 567;1492; Led; 0; 3; 2; 1500; 7; 1; NaN; NaN
29; 2; 893; 893; Led; 0; 6; 1; 1500; 7; 1; NaN; NaN
30; 2; 834; 834; Led; 0; 9; 1; 1500; 7; 1; NaN; NaN
31; 2; 702; 702; Led; 0; 12; 1; 1500; 7; 1; NaN; NaN
32; 2; 942; 942; Led; 0; 15; 1; 1500; 7; 1; NaN; NaN
33; 2; 673; 673; Led; 0; 18; 1; 1500; 7; 1; NaN; NaN
34; 2; 917; 917; Led; 0; 21; 1; 1500; 7; 1; NaN; NaN
35; 2; 767; 767; Led; 0; 24; 1; 1500; 7; 1; NaN; NaN
36; 2; 543; 543; Led; 0; 27; 1; 1500; 7; 1; NaN; NaN
37; 2; 766;1494; Led; 15; 3; 2; 1500; 7; 1; NaN; NaN
38; 2; 603; 603; Led; 15; 6; 1; 1500; 7; 1; NaN; NaN
39; 2; 858; 858; Led; 15; 9; 1; 1500; 7; 1; NaN; NaN
40; 2; 704; 704; Led; 15; 12; 1; 1500; 7; 1; NaN; NaN
41; 2; 840; 840; Led; 15; 15; 1; 1500; 7; 1; NaN; NaN
42; 2; 625; 625; Led; 15; 18; 1; 1500; 7; 1; NaN; NaN
43; 2; 795; 795; Led; 15; 21; 1; 1500; 7; 1; NaN; NaN
44; 2; 649; 649; Led; 15; 24; 1; 1500; 7; 1; NaN; NaN
45; 2; 928; 928; Led; 15; 27; 1; 1500; 7; 1; NaN; NaN
46; 2; 526;1492; Led; 30; 3; 2; 1500; 7; 1; NaN; NaN
47; 2; 793; 793; Led; 30; 6; 1; 1500; 7; 1; NaN; NaN
48; 2; 964; 964; Led; 30; 9; 1; 1500; 7; 1; NaN; NaN
49; 2; 825; 825; Led; 30; 12; 1; 1500; 7; 1; NaN; NaN
50; 2; 513; 513; Led; 30; 15; 1; 1500; 7; 1; NaN; NaN
51; 2; 698; 698; Led; 30; 18; 1; 1500; 7; 1; NaN; NaN
52; 2; 957; 957; Led; 30; 21; 1; 1500; 7; 1; NaN; NaN
53; 2; 817; 817; Led; 30; 24; 1; 1500; 7; 1; NaN; NaN
54; 2; 843; 843; Led; 30; 27; 1; 1500; 7; 1; NaN; NaN
55; 2; 646;1493; Led; 45; 3; 2; 1500; 7; 1; NaN; NaN
56; 2; 742; 742; Led; 45; 6; 1; 1500; 7; 1; NaN; NaN
57; 2; 745; 745; Led; 45; 9; 1; 1500; 7; 1; NaN; NaN
58; 2; 686; 686; Led; 45; 12; 1; 1500; 7; 1; NaN; NaN
59; 2; 790; 790; Led; 45; 15; 1; 1500; 7; 1; NaN; NaN
60; 2; 620; 620; Led; 45; 18; 1; 1500; 7; 1; NaN; NaN
61; 2; 681; 681; Led; 45; 21; 1; 1500; 7; 1; NaN; NaN
62; 2; 667; 667; Led; 45; 24; 1; 1500; 7; 1; NaN; NaN
63; 2; 988; 988; Led; 45; 27; 1; 1500; 7; 1; NaN; NaN
 
Last edited:
Physics news on Phys.org
Is the file actually comma-separated or is it semicolon-delimited as shown in your post?
 
The file is exactly like this post, I also try to change the comma in textread in an a ;
 
Then it is not a csv file.
 
and why not? It should be a csv-file... and how can I read it in another extension?
 
If it is semi colon delimited then it is NOT comma delimited. CSV stands for comma separated values and implies a comma delimited file.
 
but how can I read this? :(
 
What are the parameters in the textread command?

Do you have to define your data structure?
 
Either strip out the header and change your delimiter to a semi-colon or use C-style syntax to read the file within Matlab.
 
  • #10
I have part of the solution. I can use the following line:
[M{1:13}]=textread('ee0100.csv','%d%d%d%d%s%d%d%d%d%d%d%d%d','headerlines',9,'delimiter',';');

Problem is now that I get lines like:
M =

Columns 1 through 7

[63x1 double] [63x1 double] [63x1 double] [63x1 double] {63x1 cell} [63x1 double] [63x1 double]

Columns 8 through 13

[63x1 double] [63x1 double] [63x1 double] [63x1 double] [63x1 double] [63x1 double]

And simply use M(:,1) doesn't give me the contents but [63x1 double]. So is it possible to change that?
 

Similar threads

Replies
10
Views
3K
Replies
4
Views
2K
Replies
8
Views
3K
Replies
3
Views
3K
Replies
1
Views
2K
Replies
1
Views
2K
Replies
14
Views
2K
Replies
1
Views
2K
Back
Top