How do I change a function input with each loop iteration?

6 ビュー (過去 30 日間)
Kevin Simans
Kevin Simans 2016 年 8 月 15 日
コメント済み: Kevin Simans 2016 年 8 月 15 日
Hello,
Here's my issue. I have a folder containing 2435 files of the type '.aqa' (from an acoustic backscatter sensor). I have a function called ReadAquaScat1000.m which converts these files to .mat, which I can then work with further. I do not want to manually run the function 2435 times to convert these files, and so have tried writing a for loop. However, I am not sure how to do this because the function input must be the filename in single quotes, not including the .aqa extension, and I'm not sure how to assign a different filename with each loop iteration. My code is below. Thank you for any help you can provide!
%converts raw ABS .aqa files into .mat files that can be organized
%using ABSall.m
%first, navigate to directory containing .aqa files
filenames=dir('*.aqa');
filenames=struct2cell(filenames);
filenames=char(filenames(1,:)); %character array of filenames
filenames2=char(zeros(2435,14)); %preallocate array for filenames w/o .aqa extension
numbursts=length(filenames);
for i = 1:numbursts; %for each burst
[~,filenames2(i,:),~]=fileparts(filenames(i,:)); %create array of filenames w/o .aqa extension
file = char(filenames2(i,:)); %single filename, to be function input
ReadAquaScat1000('file','SINGLE') %convert file
end

採用された回答

Stephen23
Stephen23 2016 年 8 月 15 日
編集済み: Stephen23 2016 年 8 月 15 日
You are making your code far too complicated for such a simple task. Keep it simple, something like this should work:
S = dir('*.aqa');
for k = 1:numel(S)
[~,file] = fileparts(S(k).name);
ReadAquaScat1000(file,'SINGLE');
end
And if you really want a list with all filenames, it is easy to put them into one cell array:
names = {S.name};
Also note that it is probably simpler to pass the path as well, rather than navigate to the directory and change the current directory:
D = '.'; % path string
S = dir(fullfile(D,'*.aqa'));
for k = 1:numel(S)
[~,file] = fileparts(S(k).name
P = fullfile(D,file);
ReadAquaScat1000(P,'SINGLE');
end
  1 件のコメント
Kevin Simans
Kevin Simans 2016 年 8 月 15 日
You have all the answers! For some reason I thought the function ReadAquaScat1000 wouldn't accept a variable in place of the filename in single quotes for the input, but apparently I was wrong. Thank you very much for your help, Stephen.

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

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