Run code on multiple files within a directory?

3 ビュー (過去 30 日間)
Ross
Ross 2014 年 9 月 12 日
編集済み: per isakson 2014 年 9 月 15 日
Hi, hoping someone can help.
I have code that when run within a chosen directory, pops up a dialog box to choose file, to open through uigetfile and it runs through the code and outputs what I need to a .txt file and saves within the same directory. This is only for 1 file though, what I would like to do is for this to run through all the files in a directory in an automated fashion. I have tried a few things but I really can't get this to work and it seems to be due to how I'm using xlsread at the start.
I am using the following to read the data from the file:
[num text raw] = xlsread(uigetfile('*.extension'));
This allows me to easily use things like this to read off various values from headers etc I need:
date = num(3:2);
fileName = text(14:56);
I obviously don't want uigetfile popping up for every file to open but when I remove this I get errors complaining that xlsread can't use wildcards etc.
How can I get it so that my code cycles through all the files, runs my code then stops? I think it's something along these lines (Pseudocode):
% Get directory name
% get filename
% get number of relevant files within directory
% for i=1:number of relevant files
% run my code
% end
I just can't quite get it. Can someone help?

採用された回答

per isakson
per isakson 2014 年 9 月 12 日
Try
sad = transpose( dir( '*.extension' ) );
for file = sad
disp( file.name )
end
and see
  2 件のコメント
Ross
Ross 2014 年 9 月 15 日
編集済み: Ross 2014 年 9 月 15 日
I think I'm close with this however when I use this code:
sad = transpose( dir( '*.extension' ) );
for file = sad
disp( file.name )
[num text raw] = xlsread(sad);
my code;
end
I get this error:
Error using xlsread
Filename must be a string.
I'm assuming I need to convert the filename to a string? But I'm not sure how using the transpose function.
Ross
Ross 2014 年 9 月 15 日
編集済み: per isakson 2014 年 9 月 15 日
Ah! I figured it out!
I used this, the structures thing was confusing me:
sad = transpose( dir( '*.extension' ) );
for file = sad
disp( file.name )
FileName = (file.name)
[num text raw] = xlsread(FileName)
mycode
end
And this gives the desired action! Thanks for your help. :)

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

その他の回答 (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