MATLAB: Allowing for code to work different with file names

faqfopenMATLABtext file

I have a code that works fine but i have thus far only used it with a single test file. As a result of this i could run it while specifying the filename as well as the folder inside the code itself:
f1d = fopen(fullfile(sbd,'TEST_A.asc'),'rt');
Now that i need to apply it to multiple different files the code will not work. I have tried including a colon to read all files (as there is only one) but this doesn't work as the matlab error says there is no file name given. Can I not just specify the folder from which to select the file and the code read whatever files are in the folder?

Best Answer

Aaron, if you just want the user to pick some file instead of using dir() or sprintf() to build/get the filename, do this:
% Have user browse for a file, from a specified "starting folder."
% For convenience in browsing, set a starting folder from which to browse.
startingFolder = 'C:\Program Files\MATLAB'; % Whatever you want.....
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.*');
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a file');
if baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, baseFileName)