MATLAB: How to change a function input with each loop iteration

characterfilenamefor loopfunctioniteration

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

Best Answer

  • 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