필터 지우기
필터 지우기

Search for all mat datafiles in directory

조회 수: 19 (최근 30일)
Seb
Seb 2017년 7월 19일
편집: Stephen23 2017년 7월 19일
I want to return a cell array containing absolute file path/locations of all .mat datafiles given an initial folder location. It should search all subfolders etc. Not sure how to implement this, and if it should be done recursively...?
Any ideas?
  댓글 수: 2
Stephen23
Stephen23 2017년 7월 19일
@Seb: what version of MATLAB are you using?
Seb
Seb 2017년 7월 19일
2016b. I essentially need the output of
dir /*.mat
held in a variable

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

답변 (1개)

Stephen23
Stephen23 2017년 7월 19일
Something like this perhaps:
D = the root directory;
S = dir(fullfile(D,'**','*.mat'));
N = {S.name};
F = {S.folder};
  댓글 수: 2
Seb
Seb 2017년 7월 19일
ok this is what I have adapted it to so far:
function self = find_matfiles(self)
FileList = dir(fullfile(uigetdir(),'**','*.mat'));
amount_files = size(FileList);
amount_files = amount_files(1,1);
for i = 1:amount_files
x = char(FileList(i).name);
y = char(FileList(i).folder);
z = '\';
s1 = strcat(y,z);
s2 = strcat(s1,x);
disp(s2);
load(s2);
end
end
However it wont load the file into the workspace(or any of them)
Stephen23
Stephen23 2017년 7월 19일
편집: Stephen23 2017년 7월 19일
@Seb: Your code has "features" that should be fixed:
  • char is confusing and totally superfluous as the names are already character.
  • using fullfile is much better than hardcoding with file separators.
  • using fullfile is much better than using strcat.
  • using size is confusing: you should simply use numel.
  • self is not used anywhere, and implies that you are trying to write a MATLAB function as it were a Python class. What is self?
  • you should always load into a structure:
S = load(...);
  • variables in this function's workspace are local to that workspace. If you want the data to be available in another workspace (e.g. the base workspace) then you will need to pass it as an output argument. MATLAB variables do not magically jump between workspaces:
  • the code would be improved by following the examples shown in the documentation:
  • if you want a truly recursive dir then have a look on FEX:

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

카테고리

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