Dialog Box and File Selection

37 views (last 30 days)
jupiter
jupiter on 29 Nov 2016
Commented: Varun Gupta on 13 Apr 2021
I have a requirement wherein I have to create a dialogue box which has three inputs. But the tricky part is the inputs should be filenames/fielpaths. Is there a way to create a dialogue box with file selection option. As of now I am using 'uigetfile' to get the files one after the other. But it would be great to have a GUI screen popping up and three files selected at once. Can I combine 'uigetfile' and 'inputdlg'. A picture has been shown in the attachement. I am trying to get a file selection option at the end of the box as shown.
  2 Comments
dpb
dpb on 29 Nov 2016
I don't do GUIs but would think you could have a callback function for the dialog box that would open the file dialog when got focus...mayhaps that's what you're doing now?
If there were a way you could be certain to identify which file went which location, you could allow 'SelectionMode','multiple' and the user could make the selection of all three at one go if they're in the same working subdirectory.
Or, if there's a known set from which the user is to select (and the size of the directory isn't too large), you might consider populating a listbox and letting the selection be done that way.
With the prepackaged controls TMW has wrapped for you in Matlab don't think there's a way to do precisely what you'd like; you'd have to write a mex function for such a specialized control externally and call it, I think...
Adam
Adam on 29 Nov 2016
Edited: Adam on 29 Nov 2016
I just add a 'browse...' pushbutton at the end of each of my edit boxes which calls uigetfile.
You can have some option that multi-selects all 3 at once in addition if you want, but having only that will limit the user to having their 3 files in the same folder which seems to restrictive for general use.
uigetfile( ..., 'MultiSelect', 'on' )
will return a cell array of 3 files for you, but they will be in alphabetical order rather than the order they were selected in. The example on the documentation page suggests otherwise as their selections are in a different order, but when I try it, just just get alhpabetised.

Sign in to comment.

Answers (1)

Jan
Jan on 29 Nov 2016
Edited: Jan on 29 Nov 2016
A lot of GUIs contain a button with the string '...' to select a file. You can add this easily also and insert a uigetfile im the callback. Then the text field's 'String' property can be set accordingly.
You could add an icon also. If the GUI is created by code, not by GUIDE:
function myGUI
handles.FigH = figure;
color = [ ...
5,5,5,5,5,5,5,5,5,6,6,6,5,5,5; ...
5,5,5,5,5,5,5,5,6,5,5,5,6,5,6; ...
5,5,5,5,5,5,5,5,5,5,5,5,5,6,6; ...
5,1,1,1,5,5,5,5,5,5,5,5,6,6,6; ...
1,4,3,4,1,1,1,1,1,1,1,5,5,5,5; ...
1,3,4,3,4,3,4,3,4,3,1,5,5,5,5; ...
1,4,3,4,3,4,3,4,3,4,1,5,5,5,5; ...
1,3,4,3,4,1,1,1,1,1,1,1,1,1,1; ...
1,4,3,4,1,2,2,2,2,2,2,2,2,2,1; ...
1,3,4,1,2,2,2,2,2,2,2,2,2,1,5; ...
1,4,1,2,2,2,2,2,2,2,2,2,1,5,5; ...
1,1,2,2,2,2,2,2,2,2,2,1,5,5,5; ...
1,1,1,1,1,1,1,1,1,1,1,5,5,5,5];
BG_nil = NaN(1, 3); % Transparent
arrowColor = zeros(1, 3);
map = [0, 0, 0; ...
146, 146, 0; ...
255, 255, 0; ...
255, 255, 255; ...
BG_nil; ...
arrowColor];
[x, y] = size(color); % inlined IND2RGB
Icon = reshape(map(color, :) / 255, x, y, 3);
handles.textH = uicontrol('Style', 'Edit', 'String', '', ...
'Position', [10, 10, 300, 32], ...
'HorizontalAlignment', 'left');
uicontrol('Style', 'PushButton', 'Position', [320, 10, 32, 32], ...
'CData', Icon, 'Callback', {@myCallback, handles});
end
function myCallback(ButtonH, EventData, handles)
[FileName, FilePath] = uigetfile('*.*');
if ~ischar(FileName)
return;
end
set(handles.textH, 'String', fullfile(FilePath, FileName));
end
  1 Comment
Varun Gupta
Varun Gupta on 13 Apr 2021
Hello,
Thank you for the explanation. Now, I want to pass the selected file from the browse to the main .m file. How can we return that ?

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!