Using dir in evalc

2 views (last 30 days)
Suha
Suha on 9 Jan 2018
Commented: Suha on 9 Jan 2018
Hi all
I am trying to extract the files ending with Marksheet.csv using
evalc('dir **Marksheet.csv**')
I know that dir tells MATLAB to scan the current folder. What if my code is in a different folder (/stimulus/test/codes) and I do not want MATLAB to cd into the folder containing the Marksheet.csv files (/stimulus/test/results). Is there a way to edit
evalc('dir **Marksheet.csv**')
so that dir in this command refers to /stimulus/test/results and I can still run this command in a code stored in /stimulus/test/codes.
Thank you for your help.
Suha

Accepted Answer

Walter Roberson
Walter Roberson on 9 Jan 2018
That code is invalid.
"dir name lists files and folders that match name. When name is a folder, dir lists the contents of the folder. Specify name using absolute or relative path names. The name argument can include the * wildcard in the file name, and both the * and the wildcard in the path name. Characters next to a wildcard must be file separators."
Your code
evalc('dir **Marksheet.csv**')
uses the wildcard without being adjacent to file separators.
If you want the files ending in Marksheet.csv that are in a different directory then
resultsdir = '/stimulus/test/results';
dinfo = dir( fullfile(resultsdir, '*Marksheet.csv') );
filenames = fullfile( resultsdir, {dinfo.name} );
Notice the complete lack of evalc(). The cell array of character vectors, filenames, will have each file name fully qualified.
  1 Comment
Suha
Suha on 9 Jan 2018
Thank you Walter for such a quick response !

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!