MATLAB: Open file in folder with numerical ID

contentsdlmreadfilefolderfopenopesort

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

Best Answer

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);
Related Question