0

I want to insert an array using a graphical interface, but I don't understand why I get these errors:

Error using waitfor

Undefined function or variable 'A'.

Error using waitfor

Error while evaluating uicontrol Callback

THE CODE:

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
Schorsch
  • 7,761
  • 6
  • 39
  • 65
alinush2693
  • 37
  • 1
  • 8
  • P.S...if I delete the first line (function read())..works, but I want to use with function, because is part of a program with a lot of function and I want to return the B array to another function...thanks – alinush2693 May 20 '13 at 17:18
  • 1
    use anonymous function for the callback instead of string. – Shai May 20 '13 at 18:33
  • Well you didn't define `A` before using it. Maybe try adding `A=[]` before the first `for` loop. – JustinBlaber May 21 '13 at 05:28

1 Answers1

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
RudolfW
  • 564
  • 8
  • 13