필터 지우기
필터 지우기

Getting variables from a custom function with a GUI

조회 수: 3 (최근 30일)
Charlie Wood
Charlie Wood 2024년 1월 9일
댓글: Charlie Wood 2024년 1월 10일
I've written a function that creates a GUI requesting the user to input two dates (start and stop). I've included format checking to ensure the user has inputted things correctly.
The part I am missing is how to assign the variables in a way to allows the inputs to be accessible outside the function (i.e. when calling the function with a different script).
Any advice would be appreciated. I can repost sharing the code if that would help.
  댓글 수: 3
Stephen23
Stephen23 2024년 1월 10일
Note that DATESTR is deprecated, will be removed in the future, and is NOT recommended.
Replace:
datestr(datetime('today'), 'dd mmm yyyy HH:MM:SS.FFF')
ans = '10 Jan 2024 00:00:00.000'
with e.g.:
string(datetime('today', 'Format','dd MMM yyyy HH:mm:ss.SSS'))
ans = "10 Jan 2024 00:00:00.000"
Charlie Wood
Charlie Wood 2024년 1월 10일
Thanks for the recommendation @Stephen23. I've made the changes.

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2024년 1월 9일
Use uiwait to pause execution of the code, in this case pause until the figure is deleted, and delete the figure in the submit button callback.
function [startDate, stopDate] = dateInputGUI()
% initialize startDate and stopDate in case the user closes the figure
% without clicking the submit button. these variables need to be
% defined in order to be output. (you could also initialize them to
% defaultStartDate and defaultStopDate or something else, depending on
% what you want the outputs to be when the user closes without clicking
% submit.)
startDate = '';
stopDate = '';
% Set default start and stop dates
defaultStartDate = datestr(datetime('today'), 'dd mmm yyyy HH:MM:SS.FFF');
defaultStopDate = datestr(datetime('tomorrow'), 'dd mmm yyyy HH:MM:SS.FFF');
% Create figure and components
f = figure('Position', [400, 400, 400, 220], 'Name', 'Date Input', 'Menubar','none','NumberTitle','off');
uicontrol('Style','text','String','Expected format: dd mmm yyyy HH:MM:SS.FFF','Position',[50,180,300,20]);
uicontrol('Style', 'text', 'String', 'Start Date:', 'Position', [50, 140, 70, 20]);
startDateEdit = uicontrol('Style', 'edit', 'Position', [130, 140, 220, 20],'String',defaultStartDate);
uicontrol('Style', 'text', 'String', 'Stop Date:', 'Position', [50, 100, 70, 20]);
stopDateEdit = uicontrol('Style', 'edit', 'Position', [130, 100, 220, 20],'String',defaultStopDate);
uicontrol('Style', 'pushbutton', 'String', 'Submit', 'Position', [110, 50, 80, 30], 'Callback', @submitCallback);
% wait until the figure is deleted or uiresume(f) is called:
uiwait(f);
% Callback function for submit button
function submitCallback(~, ~)
startDate = get(startDateEdit, 'String');
stopDate = get(stopDateEdit, 'String');
% delete the figure:
delete(f);
end
end

추가 답변 (1개)

Sonesson
Sonesson 2024년 1월 9일
편집: Sonesson 2024년 1월 9일
Hello Charlie,
There are a couple of methods to accsess variables outside of their local function workspace.
The most straightforward is to simply define them as an output variable for the function
%% Script that calls the function handling the user input
[StartDate,StopDate] = FunctionThatGetsUserInputThroughGUI(SomeInput)
%StartDate and StopDate is now available in the workspace that called the
%function.
%Assume you have some function to handle the variables
%Define the output variables as [output1, output2,..., outputN]
function [StartDate, StopDate] = FunctionThatGetsUserInputThroughGUI(SomeInput)
% your code here that creates GUI resulting in the start and stopdate e.g.
StartDate = datetime('yesterday');
StopDate = datetime('today');
end
Less reccommended options would be to use assignin...
%% Script that calls the function handling the user input
FunctionWithAssignin(SomeInput)
%StartDate and StopDate is now available in the workspace that called the
%function.
function FunctionWithAssignin(SomeInput)
% your code here that creates GUI resulting in the start and stopdate e.g.
StartDate = datetime('yesterday');
StopDate = datetime('today');
assignin('caller','StartDate',StartDate)
assignin('caller','StartDate',StartDate)
end
... or global variables
%% Script that calls the function handling the user input
global StartDate StopDate
FunctionWithGlobals(SomeInput)
%StartDate and StopDate is now available in the workspace that called the
%function.
function FunctionWithGlobals(SomeInput)
global StartDate StopDate
% your code here that creates GUI resulting in the start and stopdate e.g.
StartDate = datetime('yesterday');
StopDate = datetime('today');
end
Good Luck!
  댓글 수: 2
Charlie Wood
Charlie Wood 2024년 1월 9일
Thank you for the response. I am using the first recommended method. I believe my issue is in the GUI functionality itself. My intent is to provide an easy way for the user to input a date and click a submit button. Issue is it seems things move forward without waiting for the user to click "Submit". I'm still new to functions and GUI usage so any advice is appreciated.
function [startDate, stopDate] = dateInputGUI()
% Set default start and stop dates
defaultStartDate = datestr(datetime('today'), 'dd mmm yyyy HH:MM:SS.FFF');
defaultStopDate = datestr(datetime('tomorrow'), 'dd mmm yyyy HH:MM:SS.FFF');
% Create figure and components
f = figure('Position', [400, 400, 400, 220], 'Name', 'Date Input');
uicontrol('Style','text','String','Expected format: dd mmm yyyy HH:MM:SS.FFF','Position',[50,180,260,20]);
uicontrol('Style', 'text', 'String', 'Start Date:', 'Position', [50, 140, 70, 20]);
startDateEdit = uicontrol('Style', 'edit', 'Position', [130, 140, 220, 20],'String',defaultStartDate);
uicontrol('Style', 'text', 'String', 'Stop Date:', 'Position', [50, 100, 70, 20]);
stopDateEdit = uicontrol('Style', 'edit', 'Position', [130, 100, 220, 20],'String',defaultStopDate);
uicontrol('Style', 'pushbutton', 'String', 'Submit', 'Position', [110, 50, 80, 30], 'Callback', @submitCallback);
% Callback function for submit button
function submitCallback(~, ~)
startDate = get(startDateEdit, 'String');
stopDate = get(stopDateEdit, 'String');
end
end
Stephen23
Stephen23 2024년 1월 9일
Use WAITFOR or UIWAIT.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by