필터 지우기
필터 지우기

how to call a function inside a button created in a function?

조회 수: 1 (최근 30일)
Abel Romero
Abel Romero 2017년 11월 27일
댓글: Rik 2017년 11월 27일
Hello everyone, I created a function in matlab in which when I introduce some values they are printed on some static text and I create a button that allows me to delete the row where the button is (the variables I keep in a struct). My question is, how can I use the callback of the delete button within the function? I attached the code so you can understand it better
function [c,t,v,fn,ln,btn] = addInfo(countElement,E)
for i=1:countElement
c = uicontrol('Style','text',...
'String',i,...
'Position',[20 460-30*(i-1) 20 20]);
t = uicontrol('Style','text',...
'String',E(i).Type,...
'Position',[70 460-30*(i-1) 60 20]);
v = uicontrol('Style','text',...
'String',E(i).V,...
'Position',[145 460-30*(i-1) 35 20]);
fn = uicontrol('Style','text',...
'String',E(i).Fnode,...
'Position',[210 460-30*(i-1) 35 20]);
ln = uicontrol('Style','text',...
'String',E(i).Lnode,...
'Position',[275 460-30*(i-1) 35 20]);
btn = uicontrol('Style', 'pushbutton', 'String', '-',...
'Position', [330 460-30*(i-1) 35 20],...
'Callback', @deleteInfo%%my problem is here);
end
function [ c,t,v,fn,ln,btn ] = deleteInfo( c,countElement,E )
a=str2double(get(handles.c,'String'));
E(a)=[];
countElement=countElement-1;
for i=1:countElement
c = uicontrol('Style','text',...
'String',i,...
'Position',[20 460-30*(i-1) 20 20]);
t = uicontrol('Style','text',...
'String',E(i).Type,...
'Position',[70 460-30*(i-1) 60 20]);
v = uicontrol('Style','text',...
'String',E(i).V,...
'Position',[145 460-30*(i-1) 35 20]);
fn = uicontrol('Style','text',...
'String',E(i).Fnode,...
'Position',[210 460-30*(i-1) 35 20]);
ln = uicontrol('Style','text',...
'String',E(i).Lnode,...
'Position',[275 460-30*(i-1) 35 20]);
btn = uicontrol('Style', 'pushbutton', 'String', '-',...
'Position', [330 460-30*(i-1) 35 20],...
'Callback', @deleteInfo%%my problem is here);
end

채택된 답변

Rik
Rik 2017년 11월 27일
Start with saving all the handles in one or multiple arrays when you initialize them (save them to guidata so you can get to them in a callback). Then in the delete callback you can loop through the array, moving all elements after the deleted object one level up.
  댓글 수: 2
Abel Romero
Abel Romero 2017년 11월 27일
I save it in the gui, my problem is that I don't know how to call the function in the delete button.
This is the code that I use when i call the addInfo function. First of all i add the element to the struct, and every time that I add an element i refresh the print. The function that delete the element that i selected do the same, but in this case i move all the elements one position.
Thanks for your answer!
function addElement_Callback(hObject, eventdata, handles)
% hObject handle to addElement (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global label value fnode lnode
[handles.E, handles.countElement]=add_Element( handles.E,handles.countElement,label,value,fnode,lnode );
[handles.c,handles.t,handles.v,handles.fn,handles.ln,handles.btn]=addInfo(handles.countElement,handles.E);
guidata(hObject,handles);
Rik
Rik 2017년 11월 27일
I'm sorry, but I don't really understand what it is you still need to fix.
Another point: don't use globals. Set them as fields of handles.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by