Reading in data from files in for loop

조회 수: 1 (최근 30일)
Thomas Rogers
Thomas Rogers 2019년 12월 4일
답변: JESUS DAVID ARIZA ROYETH 2019년 12월 4일
Currently I have code that reads in data files (test # 1000.001, test #1000.002 ... test # changes everyday) and takes a set number of data points and puts them in a matrix for further calculations.
%var def%
lopoff = 1034; %initial data point
sample_stopcount = 41000; %last data point
folder = uigetdir('C:\Users\me\Documents', 'Select'); %user select folder where test data collected, folder test # xxxx
[filepath, name] = fileparts(folder)
findNumb = regexp(name, '\d*','match'); %search folder name for number towards end, match it
cell2char = findNumb{1};% convert cell to char
fileNumber = str2numb(cell2char); %the test number, ie 1000, 1001, 1002...
location = 'C:\Users\me\Documents'; %main fold/dir loc
foldername = sprintf('test # %d', fileNumber);%creates folder name that changes based on test #
subfolder = 'CHANNEL'; %subfolder where data is located
%%%% Important Part-Reading in data files to a Matrix %%%%%
filenameno1 = sprintf('test # %d.001',fileNumber); %con filename using test #
filename1 = fullfile(location,foldername,subfolder,filenameno1); %constructs complete test#.001 filename via its loc
fid = fopen(filename1,'r');%open file to read
data1 = textscan(fid, '%f', sample_stopcount, 'Headerlines', lopoff); %read in data
Mydata = data1{1};
fid = fclose(fid);
filenameno2 = sprintf('test # %d.002', fileNumber);
filename2 = fullfile(location,foldername,subfolder,filenameno2);
fid = fopen(filename2, 'r');
data2=textscan(fid, '%f', sample_stopcount, 'Headerlines', lopoff);
Mydata2 =data2{1};
fid=fclose(fid);
% continues on for more data files
data_matr = [time Mydata Mydata2]; %fill up to mydata9
I was wondering if it is possible to arrange this concisely into a for loop of sorts? Can the counter for the loop be in the place of the file extension, eg, my files go .001,.002,.003... can the k=1:9 work in the sprintf so something like sprintf('test # %d' , fileNumber, '.00', numb2str(k))? Everything following should stay the same, I believe, in the for loop. Also how do I save what I read from opening one file into data_matr before closing and overwriting in a loop? Thanks and sorry for long winded question.

답변 (1개)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019년 12월 4일
%var def%
lopoff = 1034; %initial data point
sample_stopcount = 41000; %last data point
folder = uigetdir('C:\Users\me\Documents', 'Select'); %user select folder where test data collected, folder test # xxxx
[filepath, name] = fileparts(folder)
findNumb = regexp(name, '\d*','match'); %search folder name for number towards end, match it
cell2char = findNumb{1};% convert cell to char
fileNumber = str2numb(cell2char); %the test number, ie 1000, 1001, 1002...
location = 'C:\Users\me\Documents'; %main fold/dir loc
foldername = sprintf('test # %d', fileNumber);%creates folder name that changes based on test #
subfolder = 'CHANNEL'; %subfolder where data is located
%%%% Important Part-Reading in data files to a Matrix %%%%%
data_matr = time;
for k=1:9
filenameno1 = sprintf(['test # %d.00' num2str(k)],fileNumber); %con filename using test #
filename1 = fullfile(location,foldername,subfolder,filenameno1); %constructs complete test#.001 filename via its loc
fid = fopen(filename1,'r');%open file to read
data1 = textscan(fid, '%f', sample_stopcount, 'Headerlines', lopoff); %read in data
Mydata = data1{1};
fid = fclose(fid);
data_matr = [data_matr Mydata];
end

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by