Open file in folder with numerical ID

The script I'm trying to create should view all the files in a folder and then read the files according to the numerical ID. The reason I'm not using the file name is that the folder contains files which are continuously updated. I get the error: 'The file 'Myfile.spt' could not be opened because: No such file or directory'. The problem seems to be with reading the file, I've also tried using fopen but it doesn't work and gives the contents of the file as only -1.
What I have so far
fileList = dir('C:File_path');
fileList = fileList(~[fileList.isdir]);
[junk, sortorder] = sort([fileList.datenum]);
fileList = fileList(sortorder);
numfiles = numel(fileList);
y = dlmread(fileList(1).name);
Thanks for any help

 采纳的回答

You need to include the directory path when you open or read that file:
usename = fullfile('C:File_path',fileList(1).name);
y = dlmread(usename);
Otherwise how is MATLAB supposed to know where that file is in order to open it?
The best way is to specify the directory path in its own variable:
usepath = 'C:File_path';
fileList = dir(fullfile(usepath,'*.*'));
... your code
usename = fullfile(usepath,fileList(1).name);
y = dlmread(usename);

1 个评论

Thank you very much, I specified the directory as an argument like you suggested and everything worked perfectly

请先登录,再进行评论。

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by