How to extract data from a CSV file with MATLAB?

  • MATLAB
  • Thread starter Erikve
  • Start date
In summary, The conversation is about reading data from a CSV file with MATLAB, but having trouble with the code. The file is not actually comma-separated, but rather semi-colon delimited. The solution is to either strip out the header and change the delimiter or use C-style syntax to read the file within MATLAB. However, using the suggested code results in columns with [63x1 double] instead of the actual contents, so there is a need to change that.
  • #1
Erikve
18
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
  • #2
Is the file actually comma-separated or is it semicolon-delimited as shown in your post?
 
  • #3
The file is exactly like this post, I also try to change the comma in textread in an a ;
 
  • #4
Then it is not a csv file.
 
  • #5
and why not? It should be a csv-file... and how can I read it in another extension?
 
  • #6
If it is semi colon delimited then it is NOT comma delimited. CSV stands for comma separated values and implies a comma delimited file.
 
  • #7
but how can I read this? :(
 
  • #8
What are the parameters in the textread command?

Do you have to define your data structure?
 
  • #9
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?
 

1. How do I read a CSV file in MATLAB?

To read a CSV file in MATLAB, you can use the "readtable" function. This function will import the data from the CSV file and store it in a table format. You can then access and manipulate the data using MATLAB commands.

2. Can I specify the delimiter when reading a CSV file in MATLAB?

Yes, you can specify the delimiter when reading a CSV file in MATLAB. The default delimiter is a comma, but you can use the "Delimiter" parameter in the "readtable" function to specify a different delimiter, such as a tab or a semicolon.

3. How do I handle missing data when reading a CSV file in MATLAB?

When reading a CSV file in MATLAB, missing data is automatically converted to "NaN" (not a number) by default. However, you can specify a different value for missing data using the "MissingValue" parameter in the "readtable" function. You can also use the "MissingRule" parameter to specify how missing data should be handled, such as skipping the entire row or column.

4. Can I read only specific columns from a CSV file in MATLAB?

Yes, you can read only specific columns from a CSV file in MATLAB by using the "SelectedVariableNames" parameter in the "readtable" function. You can specify the column names or indices of the columns you want to import, and the resulting table will only contain those columns.

5. How do I handle headers when reading a CSV file in MATLAB?

By default, the first row of a CSV file is treated as the column headers when reading in MATLAB. However, if your CSV file does not have headers, you can use the "ReadVariableNames" parameter in the "readtable" function to specify if the first row should be treated as data or as column names. You can also specify your own custom column names using the "VariableNames" parameter.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
3K
  • Programming and Computer Science
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
350
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Nuclear Engineering
Replies
7
Views
2K
Back
Top