필터 지우기
필터 지우기

Access .txt and .csv, edit data, error statistics

조회 수: 2 (최근 30일)
Daphne PARLIARI
Daphne PARLIARI 2020년 1월 25일
댓글: Allen 2020년 1월 27일
Hi guys!
I would appreciate your help on the following:
I have several files (attached you can find two of them) that contain measured (.txt) and simulated (.csv) hourly values for some meteorological variables. I am only interested in temperature and relative humidity. The simulated period run from 01-Jul-2019 to 30-Sep-2019, while for the observations, I have a couple of months more.
What I need to do is open both files for every station (here I attached only "Airport"), access the dates that both files contain data and produce graphs and error metrics.
Any ideas please?

채택된 답변

Allen
Allen 2020년 1월 26일
A basic method for importing only the Temp and RH data into tables
filename = 'Airport.csv';
opts = detectImportOptions(filename);
opts.SelectedVariableNames = opts.SelectedVariableNames(16:17); % Columns 16 and 17 from .csv are temp and RH
data1 = readtable(filename,opts);
filename = 'Hourly Data Airport 2019.txt';
opts = detectImportOptions(filename);
opts.SelectedVariableNames = opts.SelectedVariableNames(2:3); % Columns 2 and 3 from .csv are temp and RH
data2 = readtable(filename,opts);
  댓글 수: 2
Daphne PARLIARI
Daphne PARLIARI 2020년 1월 26일
Thank you Allen, it worked! I made some alterations but it's great.
Could I do the same but with multiple files? To explain my goal, every station (Airport, Martiou etc) has two respective files (.csv and .txt) in different directories that I must import and do my work (error statistics as I mentioned above).
Is there an efficient way to do that?
Allen
Allen 2020년 1월 27일
You could assign data dynamically to a structure where the *.csv filename is used as the fieldname. This way you use a loop to handle multiple stations.
files = {'Airport.csv','Hourly Data Airport 2019.txt'
'Martiou.csv','Hourly Data Martiou 2019.txt'}; % You may need to add filepaths
for i=1:size(file,1)
[~,fn,~] = fileparts(files{i,1}); % Splits the filename into path, file, extension
fldnm = matlab.lang.makeValidName(fn); % This converts the filename into a valid string to use as a fieldname
CSV.(fldnm).opts = detectImportOptions(file{i,1});
CSV.(fldnm).opts.SelectedVariableNames = CSV.(fldnm).opts.SelectedVariableNames(16:17);
CSV.(fldnm).data = readtable(file{i,1},CSV.(fldnm).opts);
TXT.(fldnm).opts = detectImportOptions(file{i,2});
TXT.(fldnm).opts.SelectedVariableNames = TXT.(fldnm).opts.SelectedVariableNames(2:3);
TXT.(fldnm).data = readtable(file{i,2},TXT.(fldnm).opts);
end

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by