필터 지우기
필터 지우기

How to call a character saved in 'uicontrol' 'string'?

조회 수: 1 (최근 30일)
kintali narendra
kintali narendra 2017년 4월 7일
댓글: kintali narendra 2017년 4월 7일
I created a for loop using uicontrol for creating pushbuttons as number of pushbuttons vary dynamically. here is my code:
for j = 1: length(ConnectionObjectProperties(1,:))
X(j) = uicontrol('parent',Window23,'Tag','SelectedConnectionObject',...
'Style','pushbutton',...
'String',ConnectionObjectProperties(1,j),...
'Position',[300 (600-80*j) 100 30],...
'callback',@DisplayConnectionObjectProperties);
end
when ever I calling 'string' handle in the callback function, It displays all the names saved in 'ConnectionObjectProperties' ,I want to know the pushbutton which is pressed or string which is saved againt that particular pushbutton.
ConnectionObjectProperties is in cell format
can you please help me.
thank you

채택된 답변

Jan
Jan 2017년 4월 7일
It is not clear what "It displays all the names saved in 'ConnectionObjectProperties'" means. What is "it"? The shown code looks fine (see below for a tiny change), so I assume the problem is what you display later on - but this part of teh code has not been posted.
function main
ConnectionObjectProperties = {'string1', 'string2', 'string3'};
figure;
for j = 1:length(ConnectionObjectProperties)
X(j) = uicontrol('parent', Window23, 'Tag', 'SelectedConnectionObject',...
'Style', 'pushbutton',...
'String', ConnectionObjectProperties{1,j},...
'Position', [300 (600-80*j) 100 30],...
'callback', @DisplayConnectionObjectProperties);
end
end
Changes:
  1. length(ConnectionObjectProperties(1, :)) creates the vector explicitely only to measure its length. Either use length(ConnectionObjectProperties) or size(ConnectionObjectProperties, 2).
  2. Extract the string from the cell string using curly braces instead of the round parenthesis.
  3. Giving all buttons the same tag might be the reason, why you get all strings later on. Unique tags are much better, but it is much better not to use the tags for addressing at all:
function DisplayConnectionObjectProperties(hObject, EventData)
disp(hObject.String)
end
This displays the string of the selected button. Looks easy :-)
  댓글 수: 1
kintali narendra
kintali narendra 2017년 4월 7일
Thank you so much Jan simson.....I solved my problem .
thanks lot you are awesome.

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

추가 답변 (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