Let's say I have data in a csv file [1 2 3 4 5; 6 7 8 9 10;11 12 13 14 15] Let's say I don't want the first or last columns or the first row in the data. How do I selectively draw that with csvread()?

I've tried tinkering around with setting boundaries, but csv seems to only allow a start point, from which it will draw to row/column infinity.

The ideal results would be somehow getting a matrix that is [2,3,4;7,8,9;12,13,14]

2 Answers

The documentation for csvread is accurate but confusing and somewhat non-standard for matlab. The basic syntax is:

a = csvread(filename) 

And if you want to specify a range, you can use:

a = csvread(filename,R1,C1,[R1 C1 R2 C2]) 

where the R and C are the rows and columns of interest. The trick is that the row and column values are zero-indexed, which is unusual in matlab. Also note that if you are specifying a range for reading, the first components of the array argument are the same as the other arguments.

For example, consider a file "test.csv" that looks like:

1,2,3,4 1,2,3,4 1,2,3,4 1,2,3,4 

The basic read operation is:

>> a = csvread('testcsv.csv') a = 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 

If you want to start reading at column 2, specify row 0 and column 1:

>> a = csvread('testcsv.csv',0,1) a = 2 3 4 2 3 4 2 3 4 2 3 4 

And if you want to stop after column 3 while including all rows (i.e. up to row 4):

>> a = csvread('testcsv.csv',0,1,[0 1 3 2]) a = 2 3 2 3 2 3 2 3 

In your specific case:

>> a = csvread('testcsv2.csv') a = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 >> a = csvread('testcsv2.csv',1,1,[1 1 2 3]) a = 7 8 9 12 13 14 

Please refer to this documentation on csvread :

In the page, you can see various implementations of csvread() such as

M = csvread(filename,R1,C1) 

where R1 is the row offset and C1 is the column offset. You can probably try your way around with this and solve your problem.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy