MATLAB Answers

Get file name and path of the file when I browse with a button

1 view (last 30 days)
Marco Boesso
Marco Boesso on 26 Nov 2020
Edited: dpb on 26 Nov 2020
I'm writing this back because I understood that I was not clear. So, I'm having huge issues with my task to use a button on the interface to execute a function and to give back some results. Let's paste the code:
fig = uifigure('Name', 'Intro');
fig.Position = [500 460 600 150];
txt = uitextarea(fig, 'Position', [25, 70, 445, 22]);
btn = uibutton(fig, 'Text', 'Sfoglia', 'Position', [475, 70, 100, 22]);
btn.ButtonPushedFcn = @ActionButtonPushed;
What did I mean to do?
  • I create the button
  • I told the button "if I press you, you must enact this function"
So, what is this function?
function [file, path] = ActionButtonPushed(btn, txt)
[file, path] = uigetfile('*.xlsx','Selezione del file');
end
This function (where actually I want to use as an input just the fact I pressed the button) has to make me browse a file, and get the file name ("file") and the path to the file ("path"), as I usually get with uigetfile, so that then I can use these two to go on.
What does the code actually do? I press the button, the window where I choose the file opens, I select the file, and then no results are given. So I can't proceed

  4 Comments

Show 1 older comment
dpb
dpb on 26 Nov 2020
That's up to you -- the ActionButtonPushed routine now doesn't do anything but return the selected file name and path. You follow that with creating a qualified file name as Mario illustrates and then use that QFN to open the file and follow that with whatever actions are to be taken on the content of the file.
MATLAB doesn't know what you want; you have to tell it... :)
Marco Boesso
Marco Boesso on 26 Nov 2020
Actually the function doesn't even return the selected name and path, despite how I wrote it. There is no variable in the workspace with those two informations! :@

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 26 Nov 2020
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 = pwd; % or 'C:\wherever';
if ~isfolder(startingFolder)
% 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)
% Now open the file with whatever function you use to do that.

  4 Comments

Show 1 older comment
Image Analyst
Image Analyst on 26 Nov 2020
If you use GUIDE or App Designer, you can put that in the callback of a button that the user clicks on to read in your data. Better yet, just load up a listbox with all the filenames in it and let the user simply click on the name in the list.
Try these links:
Marco Boesso
Marco Boesso on 26 Nov 2020
Unfortunately I am not using GUIDE or App Designer because I already coded the program and I'm trying to create an interface. I'd be forced to write again the code, so I need to hard-code this by my own
dpb
dpb on 26 Nov 2020
Why not just add the call to uigetfile at the beginning of your code inline and go on? You don't need a pushbutton and callback unless you're trying to do much more of a GUI to just get the file dialog to open a file..
If you're really trying to build a full-fledged GUI, then probably need to follow the outlines of doing so in the examples in the doc if you're not expert.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!