如何强制Matlab在文件夹中连续读取文件? [英] How to force Matlab to read files in a folder serially?

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

问题描述

我有一个文件夹中的文件夹,编号从 writer_1 writer_20 。我写了一个代码来读取所有的文件并将它们存储在单元格中。但问题是文件不是连续读取的。

  folders = dir(Path_training); 
文件夹(ismember({folders.name},{'。','..'}))= []; %从列表中删除这两个
training = [];
for i = 1:length(folders)
current_folder = [Path_training folders(i).name'\'];





这里文件夹(1).name writer_1和文件夹(2).name 是writer_10



我知道 dir 将以资源管理器的形式返回其结果,但是有没有办法强制它进行数字?



我正在根据这些数字训练一个SVM这个问题使得它很困难。

解决方案

方法#1

 %//从你的代码
folders = dir(Path_training);
文件夹(ismember({folders.name},{'。','..'}))= []

%//将文件夹结构转换为具有全部数据来自dir
folders_cellarr = struct2cell(folders)

%//获取文件名
fn = folders_cellarr(1,:)

%//获取数字部分和排序索引
num = str2double(cellfun(@(x)strjoin(regexp(x,['\d'],'match'),''),fn(:),'uni ',0))
[〜,ind] = sort(num)

%//根据排序的索引重新排列文件夹
folders = folders(ind)

方法#2



如果你想避免 struct2cell ,这里是一种替代方法 -

 %//获取文件名
fn = cellstr(ls(Path_training))
fn(ismember(fn,{'。','..'}))= []

%//获取数字部分和排序索引
num = str2double(cellfun(@(x)strjoin(regexp(x,['\d'],'match'),''), fn(:),'uni',0))
[〜,ind] = s ort(num)

%//列出目录并根据排序的索引重新排列元素
folders = dir(Path_training);
文件夹(ismember({folders.name},{'。','..'}))= []
folders = folders(ind)

请注意, strjoin 是MATLAB Toolbox最近的一个补充。因此,如果您使用的是旧版本的MATLAB,请参阅源代码链接从MATLAB文件交换。


I have files in a folder that are numbered from writer_1 to writer_20. I wrote a code to read all the files and store them in cells. But the problem is that the files are not read serially.

folders = dir(Path_training);
folders(ismember( {folders.name}, {'.', '..'}) ) = []; %Remove these two from list
training = [];
    for i = 1:length(folders)
        current_folder = [Path_training folders(i).name '\']; 
.
.
.
.
.

Here folders(1).name is writer_1 and folders(2).name is writer_10

I know that dir will return its results as the explorer does but is there any way to force it to go numerically?

I'm training an SVM based on these numbers and this problem is making it difficult.

解决方案

Approach #1

%// From your code
folders = dir(Path_training);
folders(ismember( {folders.name}, {'.', '..'}) ) = []

%// Convert folders struct to a cell array with all of the data from dir
folders_cellarr = struct2cell(folders)

%// Get filenames
fn = folders_cellarr(1,:)

%// Get numeral part and sorted indices
num=str2double(cellfun(@(x) strjoin(regexp(x,['\d'],'match'),''), fn(:),'uni',0))
[~,ind] = sort(num)

%// Re-arrange folders based on the sorted indices
folders = folders(ind)

Approach #2

If you would like to avoid struct2cell, here's an alternative approach -

%// Get filenames
fn = cellstr(ls(Path_training))
fn(ismember(fn,{'.', '..'}))=[]

%// Get numeral part and sorted indices
num=str2double(cellfun(@(x) strjoin(regexp(x,['\d'],'match'),''), fn(:),'uni',0))
[~,ind] = sort(num)

%// List directory and re-arrange the elements based on the sorted indices
folders = dir(Path_training);
folders(ismember( {folders.name}, {'.', '..'}) ) = []
folders = folders(ind)

Please note that strjoin is a recent addition to MATLAB Toolbox. So, if you are on an older version of MATLAB, here's the source code link from MATLAB File-exchange.

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

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