필터 지우기
필터 지우기

Do calculations in csv data one csv file at the time

조회 수: 3 (최근 30일)
Martin Kneyber
Martin Kneyber 2024년 4월 27일
편집: Voss 2024년 4월 28일
I have several csv files. Want to perform calculations on the data in the csv file, for example calculating variable x. Then, I want one table with in the first column the filename of each csv file and then in the next column the calculated variable x. How can I do this? Newbie,so sorry for asking.
  댓글 수: 3
Martin Kneyber
Martin Kneyber 2024년 4월 27일
Sure, but there are a lot of files and each files has a big number of rows - hence looking for a way to automate it

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

채택된 답변

Voss
Voss 2024년 4월 27일
Something like this; adjust as necessary.
% use dir() to get info about the relevant csv files:
csv_dir = '.';
F = dir(fullfile(csv_dir,'*.csv'));
filename = fullfile({F.folder},{F.name}).';
% preallocate x. I assume it's a scalar numeric for each file
N = numel(F);
x = zeros(N,1);
% loop over the files
for ii = 1:N
% read each file (use the appropriate function with the appropriate options)
data = readtable(filename{ii});
% ...
% calculate this_x based on data
% ...
% store this_x
x(ii) = this_x;
end
% write file with file names and x values
T = table(filename,x);
writetable(T,'out.csv')
  댓글 수: 2
Martin Kneyber
Martin Kneyber 2024년 4월 28일
Thank you !!!!!
Voss
Voss 2024년 4월 28일
편집: Voss 2024년 4월 28일
You're welcome!

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

추가 답변 (1개)

Image Analyst
Image Analyst 2024년 4월 27일
See the FAQ for code samples to process a sequence of files:

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by