读取Matlab文件夹中的所有文件 [英] Reading all files in folder in matlab

查看:363
本文介绍了读取Matlab文件夹中的所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
浏览matlab文件夹中的文件

Possible Duplicate:
Loop through files in a folder in matlab

我有几个文件夹,每个文件夹中包含约50个csv文件,我必须读取每个文件并将其放在与文件相同但没有扩展名.csv的变量中.文件为2x15000矩阵.有人可以帮忙吗?我已经搜索了Internet,但对我没有任何帮助.谢谢!

I have a few folders with around 50 csv files in each and I have to read each file and place it in a variable named same as a file but without extension .csv. Files are 2x15000 matrix. Can anyone help? I've searched the Internet, but nothing works for me. Thanks!

推荐答案

这是另一种解决方案:

dd = dir('*.csv');

fileNames = {dd.name}; 

data = cell(numel(fileNames),2);
data(:,1) = regexprep(fileNames, '.csv','');

for ii = 1:numel(fileNames)    
   data{ii,2} = dlmread(fileNames{ii});
end

这将输出类似

data = 
    'test1.csv'    [2x15000 double]
    'test2.csv'    [2x15000 double]
    etc.

使用这种方法,实际上并不需要大量的变量名.在这样的情况下使用细胞阵列通常被认为是更好的方法.成千上万的变量名称方法是未完成的".

With this approach, there's really no need to have a zillion variable names flying about. Using cell-arrays in situations like these is generally considered the better way to go; the zillion-variable names approach is "not done".

这篇关于读取Matlab文件夹中的所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆