MATLAB: How to find out the image name of a randomly selected image and save it to workspace by PushButton

dicomguiimagesimages selectionmatlab guiorderpushbutton

Hello,
I have created a command that allows me to display different images from a folder to a GUI. There are a total of 4 randomly selected images from 4 different folders.
The command is:
files = dir('/Users/Documents/MATLAB/IR1/**/*.dcm')
num_files = numel(files);
filename = files(randi(num_files)).name;
image = dicomread(filename);
for image_order = randperm(num_files);
k = image_order
filename = files(k).name;
image = dicomread(filename);
end
With the PushButton the user can select the images by his own order.
My question now would be, how does the image name (of the selected image) save itself in the workspace? And how can one set it so that exactly the order appears that one has selected?
Unfortunately I don't know the commands for it, even after a long search I couldn't find an answer.

Best Answer

The code you show doesn't appear to be related to a GUI, so it's unclear how it relates to your question.
how does the image name (of the selected image) save itself in the workspace
Nothing saves itself. You have to explicitly store whatever information you want to keep in a variable.
In the code you show, if you want to keep the index of the random images you're using you have to store the output of randi and randperm rather than discarding them as soon as you don't need them:
files = dir('/Users/Documents/MATLAB/IR1/**/*.dcm')
num_files = numel(files);
chosen_singlefile = randi(num_files); %keep the chosen index around
filename = files(chosen_singlefile).name;
image = dicomread(filename);
chosen_permutation = randperm(num_files); %keep the chosen ordering around
for image_order = chosen_permutation
k = image_order
filename = files(k).name;
image = dicomread(filename);
end