简体   繁体   中英

Error using importdata in Matlab to import .txt files by reading the filenames from a cell

Here is the code (simplified):

Dir = dir('C:\Folder\SubFolder_1\Subfolder_2');
allFiles = {Dir(~[Dir.isdir]).name};

for i = 1:length(allFiles);
    data = importdata(allFiles{i});
end

The error occurs on the line with the importdata statement:

Error using importdata (line 137)
Unable to open file.

Error in Main (line 29)
data = importdata(allFiles{i});

Edit 1: From the same directory that I was in in the previous case, when I do,

allFiles = {('C:\Folder\SubFolder_1\Subfolder_2\File1.txt'),... 
            ('C:\Folder\SubFolder_1\Subfolder_2\File2.txt')};

for i = 1:length(allFiles);
    data = importdata(allFiles{i});
end

I don't get an error and the files read in fine.

Your syntax is okay (atleast it works over here).

Have you checked if your pwd (present workingdirectory) is C:\\Folder\\SubFolder_1\\Subfolder_2 when you run your statement?

If not consider something like concatenating the absolute path:

path = 'C:\Folder\SubFolder_1\Subfolder_2\';
Dir = dir(path);
allFiles = {Dir(~[Dir.isdir]).name};

for i = 1:length(allFiles)
    absoluteFileLocation = [path allFiles{i}];
    data = importData(absoluteFileLocation);
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM