MATLAB - 从目录读取文件?

发布于 2024-09-04 16:35:52 字数 927 浏览 3 评论 0原文

我希望从目录中读取文件并对每个文件迭代执行操作。此操作不需要更改文件。

我知道我应该为此使用 for 循环。到目前为止,我已经尝试过:

FILES = ls('path\to\folder');

for i = 1:size(FILES, 1);
    STRU = pdbread(FILES{i});
end

这里返回的错误向我(一个新手)建议,使用 ls() 列出目录不会将内容分配给数据结构。

其次,我尝试创建一个文件,每行包含一个文件路径,例如,

C:\Documents and Settings\My Documents\MATLAB\asd.pdb
C:\Documents and Settings\My Documents\MATLAB\asd.pdb

然后我使用以下代码读取该文件:

fid = fopen('paths_to_files.txt');
FILES = textscan(fid, '%s');
FILES = FILES{1};
fclose(fid);

此代码读取该文件,但在路径中存在空格的地方创建一个换行符,即

'C:\Documents'
'and'
'Setting\My'
'Documents\MATLAB\asd.pdb'

最终,我然后打算使用 for 循环

for i = 1:size(FILES, 1)
    PDB = pdbread(char(FILES{i}));

读取每个文件,但 pdbread() 会抛出一个错误,表明该文件格式不正确或不存在。

这是由于读入路径文件时路径的换行符分隔造成的吗?

非常感谢任何帮助或建议。

谢谢, 小号:-)

I wish to read files from a directory and iteratively perform an operation on each file. This operation does not require altering the file.

I understand that I should use a for loop for this. Thus far I have tried:

FILES = ls('path\to\folder');

for i = 1:size(FILES, 1);
    STRU = pdbread(FILES{i});
end

The error returned here suggests to me, a novice, that listing a directory with ls() does not assign the contents to a data structure.

Secondly I tried creating a file containing on each line a path to a file, e.g.,

C:\Documents and Settings\My Documents\MATLAB\asd.pdb
C:\Documents and Settings\My Documents\MATLAB\asd.pdb

I then read this file using the following code:

fid = fopen('paths_to_files.txt');
FILES = textscan(fid, '%s');
FILES = FILES{1};
fclose(fid);

This code reads the file but creates a newline where a space exists in the pathway, i.e.

'C:\Documents'
'and'
'Setting\My'
'Documents\MATLAB\asd.pdb'

Ultimately, I then intended to use the for loop

for i = 1:size(FILES, 1)
    PDB = pdbread(char(FILES{i}));

to read each file but pdbread() throws an error proclaiming that the file is of incorrect format or does not exist.

Is this due to the newline separation of paths when the pathway file is read in?

Any help or suggestions greatly apppreciated.

Thanks,
S :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

阿楠 2024-09-11 16:35:52

首先获取符合您条件的所有文件的列表:
(在本例中,pdb 文件位于 C:\My Documents\MATLAB 中),

matfiles = dir(fullfile('C:', 'My Documents', 'MATLAB', '*.pdb'))

然后按如下方式读入文件:
(这里i可以从1到文件数不等)

data = load(matfiles(i).name)

重复此操作,直到读完所有文件。


如果您可以重命名文件,则更简单的替代方案如下:-

首先保存 reqd.txt 文件。文件为1.pdb,2.pdb,3.pdb,...等。

然后在Matlab中迭代读取它们的代码如下:

for i = 1:n
    str = strcat('C:\My Documents\MATLAB', int2str(i),'.pdb'); 
    data = load(matfiles(i).name);

% use our logic here  
% before proceeding to the next file

end

First Get a list of all files matching your criteria:
( in this case pdb files in C:\My Documents\MATLAB )

matfiles = dir(fullfile('C:', 'My Documents', 'MATLAB', '*.pdb'))

Then read in a file as follows:
( Here i can vary from 1 to the number of files )

data = load(matfiles(i).name)

Repeat this until you have read all your files.


A simpler alternative if you can rename your files is as follows:-

First save the reqd. files as 1.pdb, 2.pdb, 3.pdb,... etc.

Then the code to read them iteratively in Matlab is as follows:

for i = 1:n
    str = strcat('C:\My Documents\MATLAB', int2str(i),'.pdb'); 
    data = load(matfiles(i).name);

% use our logic here  
% before proceeding to the next file

end
荒芜了季节 2024-09-11 16:35:52

我从雅虎答案复制这个!这对我有用

% copy-paste the following into your command window or your function

% first, you have to find the folder
folder = uigetdir; % check the help for uigetdir to see how to specify a starting path, which makes your life easier

% get the names of all files. dirListing is a struct array. 
dirListing = dir(folder);

% loop through the files and open. Note that dir also lists the directories, so you have to check for them.
for d = 1:length(dirListing)
    if ~dirListing(1).isdir
        fileName = fullfile(folder,dirListing(d).name); % use full path because the folder may not be the active path

        % open your file here 
        fopen(fileName)

        % do something

    end % if-clause
end % for-loop

I copy this from yahoo answers! It worked for me

% copy-paste the following into your command window or your function

% first, you have to find the folder
folder = uigetdir; % check the help for uigetdir to see how to specify a starting path, which makes your life easier

% get the names of all files. dirListing is a struct array. 
dirListing = dir(folder);

% loop through the files and open. Note that dir also lists the directories, so you have to check for them.
for d = 1:length(dirListing)
    if ~dirListing(1).isdir
        fileName = fullfile(folder,dirListing(d).name); % use full path because the folder may not be the active path

        % open your file here 
        fopen(fileName)

        % do something

    end % if-clause
end % for-loop
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文