0

我想使用图形界面插入一个数组,但我不明白为什么会出现这些错误:

Error using waitfor

Undefined function or variable 'A'.

Error using waitfor

Error while evaluating uicontrol Callback

编码:

function read()
clear all
clc

n=2;

b=50;   
a=300;
B = nan(n);
S.fh = figure('units','pixels',...
              'position',[500 500 500 500],...
              'menubar','none',...
              'numbertitle','off',...
              'name','Matrix',...
              'resize','off');
for i=1:n    
    for j=1:n
        A(i,j) = uicontrol('style','edit','units','pixels',...
                 'position',[b a 50 50],'fontsize',20,'string','',...
                 'Callback', 'B(A == gco) = str2double(get(gco, ''string''));');
       b = b+60;
    end
    b = 50;
    a = a-60;
end

S.bb = uicontrol('style','push',...
                 'units','pixels',...
                 'position',[300 10 75 50],...
                 'fontsize',14,...
                 'string','Done',...
                 'callback','close');

waitfor(S.fh)
B
4

1 回答 1

0

Instead of using callbacks for all the edit boxes separately, I recommend a single callback that reads all the values on the button. For instance:

function read()
clear all
clc

n=2;

b=50;   
a=300;
% A = zeros(n);
S.fh = figure('units','pixels',...
              'position',[500 500 500 500],...
              'menubar','none',...
              'numbertitle','off',...
              'name','Matrix',...
              'resize','off');
for i=1:n    
    for j=1:n
        A(i,j) = uicontrol('style','edit','units','pixels',...
                 'position',[b a 50 50],'fontsize',20,'string','');
                 % no callback for the edit boxes

       b = b+60;
    end
    b = 50;
    a = a-60;
end

S.bb = uicontrol('style','push',...
                 'units','pixels',...
                 'position',[300 10 75 50],...
                 'fontsize',14,...
                 'string','Done',...
                 'callback',@(~,~)(readvalues(A,n)));
                 % callback that reads all the values in one run
                 % (and closes the figure as you wanted)

waitfor(S.fh)


function readvalues(A,n)
B = zeros(n);
for i=1:n
    for j=1:n
        B(i,j) = str2double(get(A(i,j), 'String'));
    end
end
disp(B)
close
于 2013-05-21T07:37:07.493 回答