Passing a variable to a uicontrol element inside a gui
Afficher commentaires plus anciens
I have a GUI that has several elements inside it. One of them is a pushbutton that, when pressed, executes some data processing, saves the prcessed data to an .xls file, and then opens a second figure. This second figure is a much more basic GUI generated via uicontrol (rather than GUIDE) with two buttons: "Open File" and "Close". The Close button simply closes this newly created second figure. The "Open File" pushbutton is supposed to open the newly created .xls file in MS Excel (using winopen). However, no matter what I do, I cannot get the uicontrol function that creates this pushbutton to recognize the string that contains the name of the file. So every time I push the pushbutton, I just get an error saying "Undefined function or variable 'file_name'."
How can I get the file_path string (which is created previously in the main GUI) to be accessed by the uicontrol function? Below is the code for the uicontrol function:
function my_function()
%Find the size of the screen and set the position of the figure inside it
oldRootUnits = get(0,'Units');
c = onCleanup(@()set(0, 'Units', oldRootUnits));
set(0, 'Units', 'points');
screenSize = get(0,'ScreenSize');
delete(c);
pointsPerPixel = 72/get(0,'ScreenPixelsPerInch');
width.wind = 350 * pointsPerPixel;
height.wind = 75 * pointsPerPixel;
pos = [screenSize(3)/2-width.wind/2 screenSize(4)/2-height.wind/2 width.wind height.wind];
% Open File pushbutton size
width.opn_file = width.wind/2;
height.opn_file = height.wind/1.5;
% Close pushbutton size
width.cls = width.wind/3.5;
height.cls = height.wind/1.5;
% This is actually called in from the previous GUI, but I have added it
% here in lieu of posting the entire previous GUI code
file_name = 'exported_data.xls';
% Create the figure
hfig = figure(...
'Units', 'points', ...
'BusyAction', 'queue', ...
'WindowStyle', 'normal', ...
'Position', pos, ...
'Resize','off', ...
'CreateFcn','', ...
'NumberTitle','off', ...
'IntegerHandle','off', ...
'MenuBar', 'none', ...
'Tag','TMWWaitbar',...
'Interruptible', 'off', ...
'DockControls', 'off', ...
'Visible','on',...
'Name','Data export complete');
% Open File pushbutton
opn_file = uicontrol('Parent',hfig,'Style','pushbutton',...
'String','Open File',...
'Position', [(width.wind*0.1) (height.wind*0.5) width.opn_file height.opn_file],...
'Callback',@opn_file_callback,...
'UserData',file_name);
% Close pushbutton
cls = uicontrol('Parent',hfig,...
'Style', 'pushbutton',...
'String', 'Close',...
'Position', [(width.wind*1.3) (height.wind*0.5) width.cls height.cls],...
'Callback', 'close');
end
function opn_file_callback(hObject,eventdata)
hObject.UserData = file_name;
display(data)
end
Réponses (1)
function opn_file_callback(hObject,eventdata)
% hObject.UserData = file_name;
% The other way around:
file_name = hObject.UserData;
winopen(file_name);
end
Note: Defining the callback of the "Close" button as a string is not reliable. This string is evaluated in the base workspace. So if you create the variable "close" in the command window, the figure does not close anymore. Better:
uicontrol('Parent',hfig,...
'Style', 'pushbutton',...
'String', 'Close',...
'Callback', @(h,e) close(gcbf));
Catégories
En savoir plus sur Interactive Control and Callbacks dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!