MATLAB: Specify Folder Name with Variable for uigetfile

uigetfile folder

Hello again… learning a lot from these forums and thankful in advance 🙂
I am doing clinical research so I have a bunch of data for each research subject in their own folder. The subjects have numbers, but they are not necessarily sequential. Early in my script I have
subject_number = input('Enter subject number:','s');
I then want to import the names for a series of files from that folder that are in .jpg format.
Currently, I am doing this:
image1=('../*.jpg','select image 1');
image2=('../*.jpg','select image 2');
Is there any way that I can specify the folder that opens in the GUI by inputting the subject_number into that file path since subject_number is the name of the folder?
I have also tried this:
[image1 PathName]=uigetfile('','select image 1');
[image2 PathName]=uigetfile(PathName, 'select image 2');
That code is successful in opening up the folder where the previously chosen image was from on the 2nd call, but I can't see .jpg files.
This would save me a step, but more importantly make sure that I choose the files from the correct subject folder each time.
Thanks for your help! Molly

Best Answer

Try 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';
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)