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

 Accepted Answer

Stephen23
Stephen23 on 27 Jun 2017
Edited: Stephen23 on 27 Jun 2017
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 Comment

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

Sign in to comment.

More Answers (0)

Categories

Asked:

on 27 Jun 2017

Commented:

on 27 Jun 2017

Community Treasure Hunt

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

Start Hunting!