以下是将十进制数转换为目标数字系统的代码:
hf = figure('Color', [1, 1, 0], 'Position', [400 400 600 400], ...
'Name', 'Number System Conversion', 'NumberTitle', 'off', 'MenuBar', 'none');
uicontrol(hf, 'Style', 'Text', 'Units', 'normalized', ...
'Position', [0.05, 0.8, 0.45, 0.1], 'HorizontalAlignment', 'center', ...
'String', 'Input', 'BackgroundColor', [0, 1, 1]);
uicontrol(hf, 'Style', 'Text', 'Units', 'normalized', ...
'Position', [0.5, 0.8, 0.45, 0.1], 'HorizontalAlignment', 'center', ...
'String', 'Output', 'BackgroundColor', [0, 1, 1]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
uiinput = uipanel(hf, 'Position', [0.04, 0.33, 0.45, 0.45], ...
'Units', 'normalized', 'BackgroundColor', [1, 1, 0]);
uicontrol('Parent', uiinput, 'Style', 'Text', 'Units', 'normalized', ...
'Position', [0.1, 0.65, 0.25, 0.1], 'HorizontalAlignment', 'right', ...
'String', 'Decimal', 'BackgroundColor', [1, 1, 0]);
uicontrol('Parent', uiinput, 'Style', 'Text', 'Units', 'normalized', ...
'Position', [0.1, 0.25, 0.25, 0.1], 'HorizontalAlignment', 'right', ...
'String', 'Target Number System', 'BackgroundColor', [1, 1, 0]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
he1 = uicontrol(hf, 'Style', 'Edit', 'Units', 'normalized', ...
'Position', [0.25, 0.6, 0.2, 0.1], 'BackgroundColor', [0, 1, 0]);
he2 = uicontrol(hf, 'Style', 'Edit', 'Units', 'normalized', ...
'Position', [0.25, 0.4, 0.2, 0.1], 'BackgroundColor', [0, 1, 0]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
uioutput = uipanel(hf, 'Units', 'normalized', ...
'Position', [0.52, 0.33, 0.45, 0.45], 'BackgroundColor', [1, 1, 0]);
uicontrol(uioutput, 'Style', 'Text', 'Units', 'normalized', ...
'Position', [0.1, 0.5, 0.2, 0.1], 'HorizontalAlignment', 'left', ...
'String', 'Result', 'BackgroundColor', [1, 1, 0]);
ht = uicontrol('Parent', uioutput, 'Style', 'Text', 'Units', 'normalized', ...
'Position', [0.4, 0.5, 0.4, 0.1], 'HorizontalAlignment', 'center', 'BackgroundColor', [0, 1, 0]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
uicontrol(hf, 'Style', 'pushbutton', 'Units', 'normalized', ...
'Position', [0.18, 0.1, 0.2, 0.12], 'String', 'Convert', 'CallBack', @callBackFun);
uicontrol(hf, 'Style', 'pushbutton', 'Units', 'normalized', ...
'Position', [0.65, 0.1, 0.2, 0.12], 'String', 'Exit', 'CallBack', 'close(hf)');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function callBackFun(src, event)
n = str2num(get(he1, 'String'));
b = str2num(get(he2, 'String'));
if b >= 10
warndlg('Target system cannot be larger than 10!', 'Warning');
set(he2, 'String', '');
else
set(ht, 'String', num2str(NumSysConv(n, b)));
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function NumSysConv = NumSysConv(n, b)
ch1 = '0123456789';
k = 1;
while n ~= 0
p(k) = rem(n, b);
n = fix(n/b);
k = k + 1;
end
k = k - 1;
strdec = '';
while k >= 1
kb = p(k);
strdec = strcat(strdec, ch1(kb + 1: kb + 1));
k = k - 1;
end
NumSysConv = strdec;
end
问题出在上一个uicontrol的第二个问题,其回调函数是@CallBackFun,我想在同一段代码中读取和设置其他uicontrol的属性,但它会显示错误消息:"未定义的函数或变量' he1'。",这意味着它无法识别同一图中的uicontrols,如何解决这个问题?提前谢谢!