Is there a way to only read every third row of a .csv file?

조회 수: 18 (최근 30일)
Petch Anuwutthinawin
Petch Anuwutthinawin 2021년 7월 10일
편집: LO 2021년 7월 10일
I need my code to read every third row and every column of a .csv file and save it seperately. For example I need the third row, sixth row, ninth row saved but not the first and second rows.

채택된 답변

LO
LO 2021년 7월 10일
편집: LO 2021년 7월 10일
import the file and the use logic indexing, double check if it is selecting the right rows, if not just change the index array to index = 1:3:height(forcetest), I am not sure which rows you need
% filename = 'C:\YOURPATH\forcetest.csv';
filename = 'C:\Users\Livio\Downloads\forcetest.csv';
delimiter = ',';
formatSpec = '%f%f%f%[^\n\r]';
fileID = fopen(filename,'r');
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'TextType', 'string', 'EmptyValue', NaN, 'ReturnOnError', false);
fclose(fileID);
forcetest = table(dataArray{1:end-1}, 'VariableNames', {'VarName1','VarName2','VarName3'});
clearvars filename delimiter formatSpec fileID dataArray ans;
index = 1:2:height(forcetest);
selected_table = forcetest(index,:);
  댓글 수: 2
Petch Anuwutthinawin
Petch Anuwutthinawin 2021년 7월 10일
Oh sorry I mistyped, I meant that I need the rows where column 1 is equal to 2. So it would be row 3,6,9 etc but there are multiple points where it misses a 2. Therefore I cannot sort by code.
LO
LO 2021년 7월 10일
편집: LO 2021년 7월 10일
this selects rows with values equal to 2
index = table2array(forcetest(:,1))==2;

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by