uicontol callback errors in different Matlab versions

1 次查看(过去 30 天)
I have a short GUI with input fields, radio buttons and push buttons:
function []=my_gui()
S.fh = figure('units','pixels',...
'position',[100 300 600 500],...
'menubar','none',...
'name','my_gui',...
'numbertitle','off',...
'resize','off',...
'deletefcn','');
S.field = uicontrol('style','edit',...
'unit','pix',...
'position',[10 450 50 30],...
'string','');
S.radio = uicontrol('style','radiobutton',...
'unit','pix',...
'position',[25 410 120 30],...
'string','');
S.button = uicontrol('style','push',...
'unit','pix',...
'position',[10 10 80 30],...
'string','Run');
set(S.button,'callback',{@run_function,S});
function [] = run_function(varargin)
assignin('base','data_in',varargin);
S = varargin{3}; % Get the structure.
my_field = str2double(get(S.field,'string'));
my_radio=S.radio.Value;
assignin('base','data_out',[my_field,my_radio]);
The GUI executes with no problems in R2015a, but when it is executed in R2012a the following message appears:
Attempt to reference field of non-structure array.
Error in my_gui>run_function (line 28)
my_radio=S.radio.Value;
Error while evaluating uicontrol Callback
Looking at the 'data_in' and 'data_out' arrays in each version shows that in 2012a the uicontrol property in data_in{1} is no longer there. Can anyone advise on the issue, or if I require a workaround?

采纳的回答

Geoff Hayes
Geoff Hayes 2017-1-12
Richard - there were changes to how GUI elements are managed around R2014b (I think as I'm still on R2014a - see http://www.mathworks.com/help/matlab/graphics_transition/graphics-handles-are-now-objects-not-doubles.html for details) so what you can code with the later versions is not valid for earlier ones. For older versions, if you have the handle to the control, you would need to use the get function to fetch a specific property. For example, if hText1 is a handle to a text control, then you would do
myText = get(hText1,'String');
to get the string contents within this control which is like what you are doing at
my_field = str2double(get(S.field,'string'));
For the radio button, you would do
my_radio = get(S.radio, 'Value');

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by