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

28 visualizaciones (últimos 30 días)
Marco Boesso
Marco Boesso el 26 de Nov. de 2020
Editada: dpb el 26 de Nov. de 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 comentarios
dpb
dpb el 26 de Nov. de 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 el 26 de Nov. de 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! :@

Iniciar sesión para comentar.

Respuestas (1)

Image Analyst
Image Analyst el 26 de Nov. de 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 comentarios
Marco Boesso
Marco Boesso el 26 de Nov. de 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 el 26 de Nov. de 2020
Editada: dpb el 26 de Nov. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by