GUI with addition, input must be a character vector or string scalar

figure('position',[400 400 400 400]);
opbox = uicontrol('style','edit','string','0','position',[10 350 50 50]);
uicontrol('style','pushbutton','string','+','position',[70 350 30 30],'callback',@pm_cb);
opbox1 = uicontrol('style','edit','string','0','position',[110 350 50 50]);
uicontrol('style','text','string','=', 'position',[170 350 30 30]);
result = uicontrol('style','edit','string','0.0','backgroundcolor',[0.75 0.5 0],'position',[210 350 50 50]);
function pm_cb( uiobject, eventdata )
global opbox opbox1 result
x = get(opbox , 'string' );
y = get(opbox1 , 'string' );
a = str2num(x)
b = str2num(y)
z = a+b
set( result , 'string' , num2str(z) );
end
Error using str2num (line 35)
Input must be a character vector or string scalar.
Error in sfdsdf>pm_cb (line 11)
a = str2num(x)
Error while evaluating UIControl Callback.
i have this error code that pops up, i dont know what is wrong with my code. The purpose is to use input from opbox and opbox1 to create a sum.

Antworten (1)

No globals, no errors:
h=struct;
h.f=figure('position',[400 400 400 400]);
h.opbox = uicontrol('style','edit','string','0','position',[10 350 50 50]);
uicontrol('style','pushbutton','string','+','position',[70 350 30 30],'callback',@pm_cb);
h.opbox1 = uicontrol('style','edit','string','0','position',[110 350 50 50]);
uicontrol('style','text','string','=', 'position',[170 350 30 30]);
h.result = uicontrol('style','edit','string','0.0','backgroundcolor',[0.75 0.5 0],'position',[210 350 50 50]);
guidata(h.f,h)
function pm_cb( uiobject, eventdata )
h=guidata(uiobject);
[opbox,opbox1,result]=deal(h.opbox,h.opbox1,h.result);
x = get(opbox , 'string' );
y = get(opbox1 , 'string' );
a = str2double(x);
b = str2double(y);
z = a+b;
set( result , 'string' , num2str(z) );
end

Kategorien

Gefragt:

am 2 Mai 2020

Beantwortet:

Rik
am 2 Mai 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by