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

8 次查看(过去 30 天)
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 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