フィルターのクリア

Automatically open new files as they appear in a folder

3 ビュー (過去 30 日間)
Emilie
Emilie 2021 年 9 月 6 日
コメント済み: Jan 2021 年 9 月 6 日
Hi!
I am currently using the importdata function to import data from a specific file input that I have to change, everytime I have a new file.
I am wondering if there's a function that would automatically detect a new .csv file and import it in Matlab?
Thanks in advance!

採用された回答

Jan
Jan 2021 年 9 月 6 日
編集済み: Jan 2021 年 9 月 6 日
Under Windows this canbe done by .NET using a System.IO.FileSystemWatcher. But in general a simple timer is easier:
% [UNTESTED CODE]
function Observer = ObserveFolder(Pattern, Fcn)
Observer = timer('BusyMode', 'queue', ...
'ExecutionMode', 'fixedSpacing', ...
'Period', 5, ... % Number of seconds between checks
'TimerFcn', @(H, E) CheckFolder(H, E, Pattern, Fcn), ...
'UserData', [], ...
'DeleteFcn', @(H, E) disp('Observer stopped.'));
start(Observer);
end
function CheckFolder(TimerH, EventData, Pattern, Fcn)
newList = dir(Pattern);
FileList = fullfile({newList.folder}, {newList.name});
DateList = {newList.date};
oldKey = TimerH.UserData;
if isempty(oldKey)
newFile = FileList;
else
key = strcat(FileList, '?', DateList);
newFile = FileList(~ismember(key, oldKey));
oldKey = key;
end
for iFile = 1:numel(newFile)
Fcn(newFile{iFile});
end
end
Call this as:
Fcn = @(File) disp(File); % A dummy function
Obs = ObserveFolder(fullfile(Folder, '*.csv'));
... let it work and stop if by:
delete(Obs)
  2 件のコメント
Emilie
Emilie 2021 年 9 月 6 日
thank you so much, I'll try this out!!
Jan
Jan 2021 年 9 月 6 日
I try it also. If it is working, maybe it is worth to publish it in the FileExchange.

サインインしてコメントする。

その他の回答 (1 件)

Yongjian Feng
Yongjian Feng 2021 年 9 月 6 日
Implement a background thread that scans the folder?

カテゴリ

Help Center および File ExchangeSoftware Development Tools についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by