How do I change a function input with each loop iteration?
6 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Stephen23
on 15 Aug 2016
Edited: Stephen23
on 15 Aug 2016
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
More Answers (0)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!