【问题标题】:insert array using graphical interface in matlab在matlab中使用图形界面插入数组
【发布时间】:2013-05-15 07:36:38
【问题描述】:

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

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

【问题讨论】:

  • PS..如果我删除第一行 (function read())..works,但我想与函数一起使用,因为它是具有很多功能的程序的一部分,我想将 B 数组返回给另一个函数...谢谢
  • 使用匿名函数代替字符串。
  • 好吧,您在使用之前没有定义A。也许尝试在第一个 for 循环之前添加 A=[]。

标签: arrays matlab user-interface matlab-guide


【解决方案1】:

我建议不要单独对所有编辑框使用回调,而是使用单个回调来读取按钮上的所有值。例如:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 2018-06-20
    • 1970-01-01
    相关资源
    最近更新 更多