How to manipulate a series of .csv files after reading it inside a for loop?

조회 수: 2 (최근 30일)
I am trying to read a series of.csv files and then trying to extract a particular row depending on a specific value of 1st 2 columns and then writing that particular row in another .csv file. That specific row has to be extracted from each of the input files and then the output file should contain all the extracted rows. But in my code , it is taking only the last file of the series. How can I get it for all the input files? I am attaching a pic of my code. Thank you.
  댓글 수: 4
JOYA GHOSH DASTIDER
JOYA GHOSH DASTIDER 2022년 2월 8일
I was checking at different steps inside the loop. The three iterations were going on but atlast the file variable takes only the last file from data cell and execute the operation.
I am new in MATLAB. So not sure if I am using proper syntex or there may be some problem with indexing.
first the data cell storing all the .csv files in it . then I am trying to get data{i} in file variable in each iteration and to extract that one row and write it in a separate output file.
so after completing the whole loop, the output file should contain all the extracted rows in it.(example:3 rows for three input files and column same as the no of columns in the input file.)

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

채택된 답변

DGM
DGM 2022년 2월 8일
It seems to be picking up and writing all three lines to the file. The output formatting is messed up, but it's all there.
I edited the fprintf() call:
d = dir('*_rain.csv');
n = length(d);
data = cell(n,1);
for i = 1:n
data{i} = csvread(d(i).name,1,0);
file = data{i};
ind1 = file(:,1) == 91.75 & file(:,2) == 26.25;
a = file(ind1,:);
fid = fopen('sorted.txt','a+');
fprintf(fid, [repmat('%f, ',1,11) '%f\n'],a.');
fclose(fid);
end
I attached the output file.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Standard File Formats에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by