Using dir to extract the names of plexon files for upload

조회 수: 2 (최근 30일)
Caitli Newman
Caitli Newman 2019년 12월 8일
편집: dpb 2019년 12월 9일
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
Caitli Newman
Caitli Newman 2019년 12월 9일
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 2019년 12월 9일
편집: Stephen23 2019년 12월 9일
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?

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2019년 12월 9일
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 2019년 12월 9일
편집: dpb 2019년 12월 9일
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... :)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by