Read specific rows and colums from CSV file

조회 수: 379 (최근 30일)
benjamin finsås
benjamin finsås 2021년 4월 30일
편집: EmirBeg 2021년 4월 30일
Hey
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!
-Ben

채택된 답변

EmirBeg
EmirBeg 2021년 4월 30일
편집: EmirBeg 2021년 4월 30일
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.
plot(x,y1);
grid;
hold on;
plot(x,y2);
hold off;
title('...');
legend('y1','y2');
I hope this helped you. Can't compile right now hope i didn't make a mistake.
  댓글 수: 1
EmirBeg
EmirBeg 2021년 4월 30일
편집: EmirBeg 2021년 4월 30일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by