Applying function only to certain files in directory

6 ビュー (過去 30 日間)
MiauMiau
MiauMiau 2017 年 4 月 10 日
コメント済み: MiauMiau 2017 年 4 月 11 日
Hi, I have the following code, which applies the function 'load_nii' to files from the current directory which I select manually with the variables k and l:
currentFolder = pwd;
listing = dir;
allDirNames = { listing.name };
dirLA =~ [ listing.isdir ];
dirLA(1:2) = 0;
dirNames = allDirNames(dirLA);
% set k, l to choose which files to convert
k = 3;
l = 182;
% convert Niftis to Mat-Files
for idx = k:l
foo(idx) = load_nii(allDirNames{idx});
betas{idx-(k-1)} = foo(idx).img;
end
What I want however is that the function load_nii is automatically applied to all files which start with certain strings (e.g. "results"), such that I don't have to set the variables k and l manually. How can I do that?
Thanks
  4 件のコメント
Guillaume
Guillaume 2017 年 4 月 10 日
Oh yes, with the solution proposed, there's no need to filter the directories anyway.
My comment was more generic in case such code (directory listing) is used for something else where file filtering does not apply.
I don't know why mathworks decided to return these two useless entries other than for the sake of matching the antiquated behaviour of dos dir.
MiauMiau
MiauMiau 2017 年 4 月 11 日
yep I figured that...strange indeed that it is implemented like this, but my code works fine now, thanks for all the help

サインインしてコメントする。

採用された回答

dbmn
dbmn 2017 年 4 月 10 日
The simplest solution is, when you know a priori what your files are called. In that case use wildcards with *
listing = dir('results*.m');
  2 件のコメント
dpb
dpb 2017 年 4 月 10 日
And, write the function such that you pass that "magic string" to the function as a user-specified variable instead of hardcoding it into the function.
MiauMiau
MiauMiau 2017 年 4 月 10 日
Thanks, very helpful!:)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by