如何让Matlab自动按顺序输入文件? [英] How can I automatically let Matlab input the file in sequence?

查看:83
本文介绍了如何让Matlab自动按顺序输入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何让Matlab自动输入文件本身,而不是自己一个人输入?

How can I let Matlab automatically input the file itself rather than one by one myself?

我的意思是,我想放入 Sample 1.wav ,然后输出 Sample 1.png ,然后放入 Sample 2.wav ,然后输出 Sample 2.png ,然后放入 Sample 3.wav ,然后输出 Sample 3.png

I mean, I want to put Sample 1.wav and then output Sample 1.png and then put Sample 2.wav and then output Sample 2.png and then put Sample 3.wav and then output Sample 3.png

我不想输入自己1、2、3,而是让Matlab从 1到1,000

I do not want to type myself 1, 2, 3 and rather let the matlab run itself from 1 to 1,000

[y,Fs] = audioread('sample1.wav');
spectrogram(y,'yaxis')
saveas(gcf,'sample1.png')

然后

[y,Fs] = audioread('sample2.wav');
spectrogram(y,'yaxis')
saveas(gcf,'sample2.png')

然后

[y,Fs] = audioread('sample3.wav');
spectrogram(y,'yaxis')
saveas(gcf,'sample3.png')

推荐答案

要以编程方式遍历文件,请使用 dir 命令获取目录中所有文件的列表.目录文档

To programmatically iterate through files, use the dir command to get a list of all files in a directory. Documentation for dir

例如,您可以使用以下命令获取当前目录中的文件列表:

For example, you can get a list of files in the current directory with the command:

list = dir

list = 

  4×1 struct array with fields:

    name
    folder
    date
    bytes
    isdir
    datenum

在这种情况下,我在当前目录中有2个文件,另外还有2个inode('.''..').这些都存储在名为 list 的结构数组中.您可以通过以下命令查看文件列表:

In this case, I have 2 files in the current directory, with the addition of 2 inodes ('.' and '..'). These are all stored in a struct array called list. You can see the list of files by the command:

{list.name}

ans =

  1×4 cell array

    {'.'}    {'..'}    {'fileA.m'}    {'fileB.m'}


可以使用 sprintf()以编程方式生成文件名. sprintf的文档


Filenames can be generated programmatically with sprintf(). Documentation for sprintf

for i = 1:10
    sprintf("sample%d.png", i)
end

ans = 

    "sample1.png"
ans = 

    "sample2.png"
ans = 

    "sample3.png"
...


将两者合并在一起,您可以使用如下代码遍历列表中的所有文件:


Combine the two together, and you can iterate through all of the files in the list with a code like so:

list = dir; % Get files in current directory
fileList = {list.name}; % Store filenames in a cell array
fileList(1:2) = []; % Delete the inodes '.' and '..'

for i = 1:length(fileList)
    % Get current filename, use curly brackets to extract string from cell array
    currentFile = fileList{i}; 
    
    % Use sprintf() to automatically generate filenames
    saveName = sprintf("sample%d.png", i); 

    % Your code goes here
    [y,Fs] = audioread(currentFile);
    spectrogram(y,'yaxis')
    saveas(gcf,saveName)
end


如果不方便移动到目标文件的目录,则可以给 dir 命令一个目标目录: list = dir('C:/TargetDirectory/').这将为您提供该目录中的文件列表,但请注意,您必须在加载时将该目标目录添加到MATLAB路径,或将其显式添加到目标文件名.例如:


If moving to the directory of the target files is inconvenient, you can give the dir command a target directory: list = dir('C:/TargetDirectory/'). This will give you the list of files in that directory, but note that you will have to add that target directory to the MATLAB path, or explicitly add that to the target filename when loading. E.g.:

% Directory path, use double quotes, not single quotes
targetDirectory = "C:/TargetDirectory/";

currentFile = fileList{i};
currentFile = targetDirectory + currentFile; % Append path to file

% Do stuff
load(currentFile)

这篇关于如何让Matlab自动按顺序输入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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