uicontol callback errors in different Matlab versions
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
voxynn
am 12 Jan. 2017
Beantwortet: Geoff Hayes
am 12 Jan. 2017
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?
0 Kommentare
Akzeptierte Antwort
Geoff Hayes
am 12 Jan. 2017
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!