How to get the output of the uibutton?

6 visualizaciones (últimos 30 días)
faran
faran el 12 de Jun. de 2018
Respondida: Walter Roberson el 12 de Jun. de 2018
Hello,
I am using uibutton and uicheckbox but I can not get the information of the output result. I want to receive information from the selected checkbox in to a cell type and if the user is choosing button and entering new items, add those items to the library2. Below is my code:
Library={'a', 'b', 'c'};
Library2=cell(2,1);
fig = uifigure('Position',[500 200 500 600]);
pnl1 = uipanel(fig,'Title','Library','FontSize',12,...
'BackgroundColor','white',...
'Position',[20 70 220 520]);
for i=1:length(Library)
checkboxL{i} = uicheckbox(pnl1, 'Text',Library{1,i},...
'Value', 0,...
'Position',[10 390-(i-1)*30 100 100],...
'ValueChangedFcn', @(checkboxL, event) cBoxChanged3(checkboxL));
end
btn = uibutton(fig,'push',...
'Position',[200, 20, 100, 30],...
'ButtonPushedFcn', @(btn,event) addnewlibrary(btn));
function cBoxChanged3(checkboxL)
j=1;
for i=1:length(checkboxL)
val = checkboxL{i}.Value;
if val
Library2{j}=checkboxL{i}.Text;
j=j+1;
end
end
end
function addnewlibrary(btn)
x = inputdlg('Tissue Library',...
'Add Library', [1 50]);
Expr = ';';
str1=x{1};
Library2 = regexp(str1,Expr,'split');
end
So when I choose checkbox I will receive this error:
Cell contents reference from a non-cell array object.
Error in cBoxChanged3 (line 5)
val = checkboxL{i}.Value;
Error in @(checkboxL,event)cBoxChanged3(checkboxL)
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 352)
Error while evaluating CheckBox PrivateValueChangedFcn.
and when I am pressing button, it goes through the code in "addnewlibrary" function but when I call the library2 it shoes that it is empty.

Respuestas (1)

Walter Roberson
Walter Roberson el 12 de Jun. de 2018
Change
for i=1:length(Library)
checkboxL{i} = uicheckbox(pnl1, 'Text',Library{1,i},...
'Value', 0,...
'Position',[10 390-(i-1)*30 100 100],...
'ValueChangedFcn', @(checkboxL, event) cBoxChanged3(checkboxL));
end
to
nL = length(Library);
for i = 1:nL
checkboxL{i} = uicheckbox(pnl1, 'Text',Library{1,i},...
'Value', 0,...
'Position',[10 390-(i-1)*30 100 100]);
end
cb = @(varargin) cBoxChanged3(checkboxL);
for i = 1:nL
set(checkboxL{i}, 'ValueChangedFcn', cb);
end
Although it might look like those two parts could be merged, they cannot be merged. When you construct the anonymous function referring to checkboxL, a copy of checkboxL as it is right then is taken. If you were still in the middle of filling in the various checkboxL{i} then it would be the partly-filled version that was recorded. So you need to postpone "capturing" the value of checkboxL until after all of the uicheckbox entries have been recorded.

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by