Info

Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.

All is OK in lines 99 to 101 in the code below when the plot subfunction is called from the main function but the axes handle and tag seem to disappear when it is called from the slider callback. Why??

1 Ansicht (letzte 30 Tage)
function GUITest
%GUITEST is a test of a problem with handle scope in a GUI.
%V1 05/07/15
clc; clear; close all
% Fix some adjustable values needed to create the GUI:
slider1Info.Min = 0;
slider1Info.Max = 10;
slider1Info.Default = 5;
% Set initial slider position:
slider1Info.Coef = slider1Info.Default;
% Set some user data:
x = 0:0.01:10;
% Create hidden figure window.
% Note the method for passing parameters to callback functions.
hFig1 = figure( ...
'Tag','figure1',...
'MenuBar','none',...
'NumberTitle','off',...
'Name','GUI Test',...
'UserData',x,...
'Visible','off');
% Create figure components Some axes and a panel):
hAxes1 = axes( ...
'Parent',hFig1,...
'Tag','axes1');
% Note the axes of the figure are defines here along with a tag.
hPanel1 = uipanel( ...
'Parent',hFig1,...
'Tag','panel1');
% Create panel components:
% Text:
hText1 = uicontrol( ...
'Parent',hPanel1,...
'Style','Text',...
'Tag','text1',...
'String','Coefficient:');
hText2 = uicontrol( ...
'Parent',hPanel1,...
'Style','Text',...
'Tag','text3',...
'String',slider1Info.Min);
hText3 = uicontrol( ...
'Parent',hPanel1,...
'Style','Text',...
'Tag','text4',...
'String',slider1Info.Max);
% Active objects:
hSlider1 = uicontrol( ...
'Parent',hPanel1,...
'Style','Slider',...
'Tag','slider1',...
'Min',slider1Info.Min,...
'Max',slider1Info.Max,...
'Value',slider1Info.Coef,...
'Callback',@slider1Callback);
% Change units to normalized.
set([hAxes1,hPanel1,hText1,hText2,hText2,hSlider1],...
'Units','normalized')
% Set axes and uicontrol positions, normalized coordinates.
% Note the controls in the panel are located with respect to the
% frame, not the figure containing the frame.
% Axes position:
set(hAxes1,'Position',[0.1 0.5 0.4 0.4]) % Normalized to figure.
% Panel position and title:
set(hPanel1,'Position',[0.55 0.1 0.4 0.85]) % Normalized to figure.
% Panel title:
set(hText1,'Position',[0.125 0.9 0.2 0.04])
% Coefficient slider:
set(hText2,'Position',[0.125 0.8 0.04 0.04],...
'HorizontalAlignment','left')
set(hSlider1,'Position',[0.165 0.8 0.4 0.04])
set(hText3,'Position',[0.565 0.8 0.4 0.04],...
'HorizontalAlignment','left')
% Save the handles structure in guidata for later recovery of tags:
appHandles = guihandles(hFig1);
% Save appHandles in guidata for later reference.
guidata(appHandles.figure1,appHandles)
% Make the GUI visible.
set(hFig1,'Visible','on')
% Plot or other action in subfunction:
plotEquation(appHandles.figure1)
function plotEquation(hObject)
% Recover handles:
appHandles = guidata(hObject);
% The first time this is executed, appHandles.axes1 exists.
% However, when plotEquation is called from the slider callback,
% axes1 has disappeared.
% Plot chosen equation with given coefficient.
coef = get(appHandles.slider1,'Value');
x = get(appHandles.figure1,'UserData');
y = sin(coef*x) + cos((10-coef)*x);
% hLine1 = plot(appHandles.axes1,x,y);
% This works on setting up but does not work on plotting from the
% slider callback function:
hLine1 = plot(gca,x,y);
% This always works.
set(hLine1,'Tag','line1');
% Set title using the equation to be plotted:
hTitle1 = title(sprintf('sin(%0.4gx)+cos(%0.4gx)',coef,10-coef));
set(hTitle1,'Tag','title1');
% Save the handles structure:
appHandles = guihandles(appHandles.figure1);
guidata(appHandles.figure1,appHandles)
function slider1Callback(~,~)
% Slider 1 callback.
% Modifies the coefficient and updates the current value box.
% Get handles:
appHandles = guidata(gcbo);
plotEquation(appHandles.figure1);

Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by