Issues with passing uitable handle to a function

Hello, I have 2 uitables that I want positioning using subplot notation hence:
ax1=subplot(2,2,1);
cnames={'x','ESF'};
% Create the uitable
t1 = uitable(f1,'Data',ESF',...
'ColumnName',cnames,...
'ColumnWidth',{50});
ax1,plot(3); pos = get(ax1,'position'); delete(ax1)
set(t1,'units','normalized'); set(t1,'position',pos)
ax2=subplot(2,2,2);
cnames={'x','offset'};
% Create the uitable
t2 = uitable(f1,'Data',offsets,...
'ColumnName',cnames,...
'ColumnWidth',{50});
ax2,plot(3); pos = get(ax2,'position'); delete(ax2)
set(t2,'units','normalized'); set(t2,'position',pos)
I also add a popup menu
popup = uicontrol('Style', 'popup',...
'String', {'ESF','Offsets'},...
'Value',1,...
'Position', [10 0 80 30],...
'Callback', @(src,evt) getAscii( src, evt,handles ));
Now depending on what the user selects, I want to copy the selected uitable contents to clipboard:
function getAscii(source,event,handles)
%cla(ax);
val = source.Value;
%indx = source.String;
switch val
case 1
disp('ESF')
d=get(t1,'data')
case 2
disp('Offsets')
d=get(t2,'data')
otherwise
disp('None')
end
%Copy to clipboard
size_d = size(d);
str = '';
for i = 1:size_d(1)
for j = 1:size_d(2)
if j == size_d(2)
str = sprintf('%s%f',str,d(i,j));
else
str = sprintf('%s%f\t',str,d(i,j));
end
end
str = sprintf('%s\n',str);
end
clipboard ('copy',str);
However, i'm not sure how to pass in the handles of ALL the uitable

댓글 수: 5

Rik
Rik 2018년 4월 3일
In your callback, you need to load the handles struct with guidata. If you want to use it, you will first have to save data to it with guidata(handle_to_figure_or_child_object,handles)
Jason's "Answer" moved here:
OK I have tried :
t1 = uitable(f1,'Data',ESF',...
'ColumnName',cnames,...
'ColumnWidth',{50});
guidata(t1);
then calling it still gives
Reference to non-existent field 't1'.
Rik
Rik 2018년 4월 3일
Have you read the doc for guidata?
Jason - it looks like you have programmatically created your GUI, so why not nest your function and callbacks in the main function so that they have access to the variables (in this case the handles) declared in the main function? See Nested Functions for more details.
Jason
Jason 2018년 4월 3일
편집: Jason 2018년 4월 3일
So I got the following working:
f1=figure
myhandles.table1 = t1;
% Save the structure
guidata(f1,myhandles)
and then in my functionI can access the selected table contents as:
d=get(myhandles.table1,'data')
thanks Rik. Also Geoff, I will look into your suggestion also, thanks

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

답변 (0개)

카테고리

도움말 센터File Exchange에서 App Building에 대해 자세히 알아보기

질문:

2018년 4월 3일

편집:

2018년 4월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by