matlab 一次读取多个文件
时间: 2024-11-03 16:19:18 浏览: 160
在MATLAB中,一次性读取多个文件可以使用`cellfun`或`dir`函数结合`readtable`、`load`或其他数据读取函数。这里有两个常见方法:
1. **使用`dir`函数 + `readtable`**:
```matlab
folder_path = 'your_folder_path'; % 文件夹路径
file_names = dir(fullfile(folder_path, '*.csv')); % 获取所有csv文件名
data_cells = cellfun(@(f) readtable(fullfile(folder_path, f.name)), file_names, 'UniformOutput', false);
```
这将返回一个cell数组,每个元素对应一个文件的表格数据。
2. **使用`glob`函数**:
```matlab
file_pattern = fullfile(folder_path, '*.*'); % 通配符匹配任意文件
all_files = glob(file_pattern);
data_cells = parallel.gpuarray.zeros(numel(all_files), 1); % 初始化一个GPUArray
parfor i = 1:numel(all_files)
data_cells(i) = load(all_files{i}); % 可能会用到parfor并行读取
end
```
3. **使用`datastore`对象** (适用于大量文件):
```matlab
datastore_path = 'your_folder_path';
ds = datastore(datastore_path, 'IncludeSubfolders', true, 'FileExtensions', {'*.txt', '*.csv'});
files = read(ds);
data = arrayfun(@(f) load(fullfile(ds.Files{f})), 1:length(files));
```
以上方法都允许你批量处理多个文件。记得替换`folder_path`为你实际的文件夹路径。
阅读全文
相关推荐


















