Get file name and path of the file when I browse with a button
11 次查看(过去 30 天)
显示 更早的评论
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 个评论
dpb
2020-11-26
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... :)
回答(1 个)
Image Analyst
2020-11-26
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 个评论
dpb
2020-11-26
编辑:dpb
2020-11-26
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!