Using dir to extract the names of plexon files for upload

1 view (last 30 days)
Hey I am trying to write a script that will scroll through all my folders and save the names of the plexon files I want. However matlab doesn't recognize the plexon file and thus keeps saving as an empty structure array and I don't know how to fix it.
D = dir;
D = D(~ismember({D.name}, {'.', '..'}));
for k = 1:numel(D)
currD=D(k).name
cd(currD)
dinfo=dir('day1')
end
dinfo =
0×1 empty struct array with fields:
name
folder
date
bytes
isdir
datenum
  3 Comments
Caitli Newman
Caitli Newman on 9 Dec 2019
I believe .plx is the extension but if I try and do something like dir .plx it returns no files. I haven't worked with plexon files before so it has been a bit of a learning curve.
Stephen23
Stephen23 on 9 Dec 2019
Edited: Stephen23 on 9 Dec 2019
Using cd like that is unlikely to work correctly.
Use absolute/relative filepaths instead, generating them using fullfile.
Is 'day1' supposed to be the name of a subfolder or a file?
Where do you specify the file extensions that you want to match?

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 9 Dec 2019
cd(currD)
dinfo=dir('day1')
Don't do that. Instead
dinfo = dir(fullfile, currD, 'day1');
dinfo(ismember({dinfo.name}, {'.', '..'})) = [];
filenames = fullfile({dinfo.folder}, {dinfo.name});

dpb
dpb on 9 Dec 2019
Edited: dpb on 9 Dec 2019
targetDir='WhteverisDirNeeded';
d=dir(fullfile(targetDir, '*.plx')); % return all files of desired extension
for i=1:numel(d)
fname=fullfile(d(i).folder},d(i).name); % build qualified filename
...
Using the specific file extension will return only those files wanted. As others say, use a path to the desired directory instead of trying to cd all over the place. You don't provide enough info to know whether your location is relative to working directory or not so the minimum you can get away with isn't possible to say for the search directory. dir() returns a fully-qualified directory regardless of whether the search is absolute or relative.
You don't have to worry about the . or .. entries with this way with the one caveat you've not named a directory with a .plx extension. If you have, you deserve the grief... :)

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!