简体   繁体   中英

matlab using SizeChangedFcn doesn't call function using guide

I want to implement some code so when the user resizes the window I can call a function. I am using matlab 2018 and they recommend using the SizeChangedFcn instead of ResizeFcn. I haven't messed with callbacks too much, but I am having trouble getting my gui to call the SizeChangedFcn each time the window is resized. Currenlty, the function isn't called at all.

function varargout = firstgui(varargin)

gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ... 
                   'gui_OpeningFcn', @firstgui_OpeningFcn, ...
                   'gui_OutputFcn',  @firstgui_OutputFcn, ...
                   'SizeChangedFcn', @resizeui, ... % <-- added this line
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end


function resizeui(hObject,event)
        disp("Hi");

Edit:

I should mention that I am using a program that relies heavily on guide so I am stuck with that implementation.

Here is what is in "gui_mainfcn"

function varargout = gui_mainfcn(gui_State, varargin)
% GUI_MAINFCN Support function for creation and callback dispatch of GUIDE GUIs. 
%   GUI_MAINFCN is called from inside MATLAB code files generated by GUIDE to handle
%   GUI creation, layout, and callback dispatch.
%
%   See also: GUIDE.

%   GUI_MAINFCN provides these command line APIs for dealing with GUIs
%
%      UNTITLED, by itself, creates a new UNTITLED or raises the existing
%      singleton*.
%
%      H = UNTITLED returns the handle to a new UNTITLED or the handle to
%      the existing singleton*.
%
%      UNTITLED('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in UNTITLED.M with the given input arguments.
%
%      UNTITLED('Property','Value',...) creates a new UNTITLED or raises the
%      existing singleton*.  Starting from the left, property value pairs
%      are
%      applied to the GUI before untitled_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to untitled_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".

%   Copyright 1984-2015 The MathWorks, Inc.

gui_StateFields =  {'gui_Name'
    'gui_Singleton'
    'gui_OpeningFcn'
    'gui_OutputFcn'
    'SizeChangedFcn' %added this line as well does not work yet
    'gui_LayoutFcn'
    'gui_Callback'};

I found the solution here:

https://www.mathworks.com/help/matlab/creating_guis/gui-options.html

Access the dialog box from the GUIDE Layout Editor by selecting Tools > GUI Options.

Other (Use SizeChangedFcn) — Program the UI to behave in a certain way when users resize the figure window.

When selecting "SizeChangedFcn" in the gui options it created a function for me.

function figure1_SizeChangedFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
disp("Hi");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM