【发布时间】:2021-04-20 15:48:25
【问题描述】:
我为此寻找了很多,但没有找到任何东西。一般来说,我对 matlab 和正则表达式非常陌生。
我的问题是,有一个目录路径“dir”,其中只有一个 .txt 文件。但是我不知道 txt 文件的文件名。我想加载这个文件。
我尝试了多种方法,但找不到解决方案。
foo = load(fullfile(dir, '-regexp', '*.txt'))
感谢您的帮助!
【问题讨论】:
我为此寻找了很多,但没有找到任何东西。一般来说,我对 matlab 和正则表达式非常陌生。
我的问题是,有一个目录路径“dir”,其中只有一个 .txt 文件。但是我不知道 txt 文件的文件名。我想加载这个文件。
我尝试了多种方法,但找不到解决方案。
foo = load(fullfile(dir, '-regexp', '*.txt'))
感谢您的帮助!
【问题讨论】:
该语法对fullfile 无效,而dir 是一个内置函数,您似乎将其用作变量...这里有一些更清晰的东西,当您有给定folder 中的单个txt 文件
folder = 'my\folder\path\';
files = dir( fullfile( folder, '*.txt' ) );
if numel( files ) ~= 1
error( 'More or less than one .txt file found!' );
end
filepath = fullfile( files(1).folder, files(1).name );
foo = load( filepath ); % load is designed for .mat files, if your .txt contains anything
% non-numeric then you may want something more like readtable here...
【讨论】: