Read specific rows and colums from CSV file

139 views (last 30 days)
benjamin finsås
benjamin finsås on 30 Apr 2021
Edited: EmirBeg on 30 Apr 2021
Quick question: How do i read a specific colum, and within that colum i want to read x amounts of rows?
This is how far ive gotten on the code:
The thing here is i want to read the marked colums from this csv file, so colum A, B and D (see picture). Also i need x1-x2 amount of rows in colum B, and y1-y2 amount of rows in colum D, because D has more rows than B:
I probably want to start from row 2 aswell.
I want to plot both in the same plot, so i can observe the difference.
Thanks for the help!

Accepted Answer

EmirBeg on 30 Apr 2021
Edited: EmirBeg on 30 Apr 2021
I can show you how i would do.
A = readtable('..');
x = A(:,1);
y1 = A(:,2);
y2 = A(:,4);
In your code y1 and y2 are the same column. If your column 2 has less data that is NAN so it can't be plotted you can just find out the difference and delete the last rows in x and y2.
s1 = size(A,1) % rows in column A
s2 = size(A,2) % rows in column B
s4 = size(A,4) % rows in column D
Now you can just choose the rows you need.
x = x(2:s1-(s1-s2),1); % extract everything from second to last relevant row
y2 = y2(2:s4-(s4-s2),1);
Now you have all 3 columns in your Workspace and they all have the same length. I usually convert these tables to arrays with table2array and str2double, so i could use plot(), but i'm sure there a better options like stackedplot or something.
If you converted them to doubles you can plot them in the same plot like this. If you stay with your tables, i don't know how to properly plot them.
hold on;
hold off;
I hope this helped you. Can't compile right now hope i didn't make a mistake.
  1 Comment
EmirBeg on 30 Apr 2021
P.S. I'm sure there are more elegant and efficient ways to solve this, but as i said, that's how i would do.
Oh, and if you don't get proper data in your table you need to definde your delimiters in the opts file.

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!