How can I Run the Code On Multiple data Files (.txt) sequentially from a folder

조회 수: 2 (최근 30일)
Hi All,
I would like to write a script that will open a group (10 or 20 files at once w.r.t time sequence) of .txt files from a folder (contain some thousands of data files). So far the following script sound good but each time I need to select the 10 or 20 files manually. Is there any way to read the 10 or 20 files automatically and in this way I can read all the files inside the folder? I think there should be a smart way in matlab, Can anybody please help me to get me out from here?
Thanks in advance
[file_list, path_]=uigetfile('*.txt','Grab the file first','files Want to Select automatic','Multiselect','on');
for i=length(file_list)
original_path =[path, file_list(i)]
end
N.B. I use Matlab 2018 version

채택된 답변

Walter Roberson
Walter Roberson 2021년 3월 26일
per_group = 10;
p = dir('CTS_main-128361*.txt');
nFiles = numel(p);
for base_k = 1:per_group:nFiles
group_end = min(nFiles, base_k+per_group-1);
group_size = group_end - base_k + 1;
A_cat = cell(group_size, 1);
for k = base_k : group_end
A = dlmread(p(k).name);
A_cat{k} = cell2mat(A);
end
A = vertcat(A_cat{:});
%do something with this group of 10
end
  댓글 수: 15
SA
SA 2021년 3월 28일
편집: SA 2021년 3월 30일
I want to see my xlabel as "1 sec mean pulse from 04-Sep-2010 17:50:00 UTC (1283619000=timestamp)". Can you please help how the script look like? num2str doesn't work....for recalling 'timestamp'- it would be different. Could you please help me in this regard?
The error message comes "Error using sprintf
Unable to convert 'datetime' value to 'int64'."
Thanks in advance
title(sprintf('(%d-%d)-th Files Avg. 1s Pulse:2nd Sensor',base_k, base_k+per_group-1));
xlabel(sprintf('1s mean Pulse from UTC time #(%d to %d)',utcTimeStamp(1st of base_k), utcTimeStamp(end of base_k)); % how to insert this "A_timestamp" value in xlabel
%% How to see the xlabel as "1s mean Pulse from UTC time # (04-Sep-2010 20:20:00 to 04-Sep-2010 22:50:00)"
Walter Roberson
Walter Roberson 2021년 3월 30일
xlabel(sprintf('1s mean Pulse from UTC time #(%d to %d)', posixtime(utcTimeStamp(1st of base_k)), posixtime(utcTimeStamp(end of base_k)));

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

추가 답변 (1개)

Mohammad Sami
Mohammad Sami 2021년 3월 25일
편집: Mohammad Sami 2021년 3월 26일
From your question I assume that you want to essentially read all the files, not just 10-20 files.
You can essentially use uigetdir to get the folder that you want to load the files from.
Then use the dir function to list all the txt files inside the selected folder.
p = uigetdir; % updated var from path to p
files = dir(fullfile(p,'*.txt'));
data = cell(length(files),1);
for i = 1:length(files)
filepath = fullfile(files(i).folder,files(i).name);
data{i} = readtable(filepath); % assuming your data can be read with readtable.
end
% do something with your data.
  댓글 수: 5
SA
SA 2021년 3월 25일
p=dir('CTS_main-128361*.txt');
nFiles=numel(p);
A_cat=cell(nFiles,1);
for k=1:nFiles
A=dlmread(p(k).name);
%A=textscan(p(k).name, '%f %f')
A_cat{k} = cell2mat(A); disp(size(A_cat))
end
A = vertcat(A_cat{:});
with this code I can call 10 files (e.g. ...61000, ....619000........ ), but how can I call the next 10 files, and so on?
Mohammad Sami
Mohammad Sami 2021년 3월 26일
p = uigetdir; % changed var from path to p
% based on your comments assuming your file names are like
% CTS_main-1234567890.txt
files = dir(fullfile(p,'CTS_main-*.txt'));
files = struct2table(files);
files.id = str2double(extractBetween(files.name,'CTS_main-','.txt');
files.g = floor(files.id / 10000); % create file grouping. adjust groups here
ug = unique(g);
for j = 1:length(ug)
% load one group of data
k = files.g == ug(j);
filesg = files(k,:);
data = cell(length(filesg),1);
for i = 1:length(filesg)
filepath = fullfile(filesg.folder{i},filesg.name{i});
data{i} = readtable(filepath); % assuming your data can be read with readtable.
end
data = vertcat(data{:});
% do something with the current group of data
end

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

카테고리

Help CenterFile Exchange에서 Chemistry에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by