-1

Hi I am designing a gui in which the user will press a button to select images and then display them in the axes I have created.

I do know how to display one image on a specific axes but how could I select and display more than one ?

Thanks

user3755632
  • 381
  • 1
  • 2
  • 20
  • That would be perfect and the concept is what I had in my head ! However, the filename{k} doesn't work on GUI. Keeps throwing errors as the filename is a string and not a cell array, therefore I cannot access the images in it. Any other idea on how to solve this ?! Thats the main problem :( – user3755632 Apr 21 '15 at 12:06
  • Okay basically the problem is that a cell array isn't created and i keep getting this error when I try to display an image by using the filename{k} index: Cell contents reference from a non-cell array object. I have literally tried everything...even used your code exactly as you have written it but the problem is still there. – user3755632 Apr 21 '15 at 14:38

1 Answers1

3

if the files are in the same folder you can use:

[filename, pathname, filterindex] = uigetfile( {file_types_to_display}, 'Window_Title', 'MultiSelect', 'on');

this will produce a cell with the selected file name as well as the paths. you can then use strcat or similar to produce the full path of the files to then use load in order to add them to the workspace.

uigetfile doesn't work with files in multiple folders.

another option is to do some sort of recursive search to list all files in the folder based on file extension or name and then proceed to load the ones you want. this way you would have all files in the folder and subfolder so you can use images in multiple folders. search MATLAB Exchange for recursive file search code.

EDIT 1: to display the selected images by using uigetfile, you need to put the code to select and display the images beneath the push-button callback generated by guide.

function pushbutton1_Callback(hObject, eventdata, handles)
%multi-file selection
[filename, pathname, ~] = uigetfile({  '*.jpg'}, 'Pick 3 files',...
'MultiSelect', 'on');

%generation of strings containing the full path to the selected images.
img1 = strcat(pathname, filename{1});
img2 = strcat(pathname, filename{2});
img3 = strcat(pathname, filename{3});

%assign an axes handle to preced imshow and use imshow to display the images
% in individual axes.
axes(handles.axes1);
imshow(img1);
axes(handles.axes2);
imshow(img2);
axes(handles.axes3);
imshow(img3);

the above code lets you select any number of images from a folder however it only shows the first 3 images in the generated cell 'filename'. to be able to select as many images as you want then scroll through them you need two more push-button callbacks for forward and back then..

function pushbutton1_Callback(hObject, eventdata, handles)
%assign global variable so the other pushbuttons can access the values
global k num_imgs pathname filename

%uigetfile in multiselect mode to select multiple files
[filename, pathname, ~] = uigetfile({'*.jpg'},...
    'Pick 3 files',...
    'MultiSelect', 'on');
%initialisation for which image to display after selection is made.
k = 1;
%dicern the number of images that where selected
num_imgs = length(filename);
%create string of full path to first image in selection and display
%the image
img1 = strcat(pathname, filename{k});
axes(handles.axes1);
imshow(img1);

function pushbutton2_Callback(hObject, eventdata, handles)
global k num_imgs pathname filename
%using global values of k create if statement to stop scrolling beyond the
%number of available images.
if k > num_imgs
    %if the index value of the image to be displayed is greater than the
    %number available default to the last image in the list
    k = num_imgs;
else
    %iterate upwards with each click of pushbutton2 to display the next
    %image in the series.
    k = k + 1;
    img1 = strcat(pathname, filename{k});
    axes(handles.axes1);
    imshow(img1);
end

function pushbutton3_Callback(hObject, eventdata, handles)
global k num_imgs pathname filename
if k < 1
    %if the index value of the image to be displayed is less than 1
    %default to the first image in the list
    k = 1;
else
    k = k - 1;
    img1 = strcat(pathname, filename{k});
    axes(handles.axes1);
    imshow(img1);
end

EDIT2: @user3755632 i'm not sure i understand. filename is a string and you can feed strings into imshow to display the image as long as the string includes the file name and the path if it is located outside the working directory. are you not using uigetfile to select the images? (can't comment on question due to lack of reputation)

bilaly
  • 536
  • 3
  • 12
  • This doesn't answer the question. The OP is asking how to **display** multiple images. You are talking about how to **load** images to the MATLAB workspace - not the same thing. – rayryeng Apr 21 '15 at 00:08
  • i apologise i tried to use initiative since the OP knows how to display one image and displaying one image or multiple is the same so i assumed the root of the problem was loading multiple images with a single click of the button in the GUI. - feel free to answer the question in my place. – bilaly Apr 21 '15 at 00:13
  • No problem. Actually you are correct. This only solves half of the problem. The displaying isn't addressed in your answer though... – rayryeng Apr 21 '15 at 00:14