Passing data from External function to main GUIDE function

조회 수: 2 (최근 30일)
James Ang
James Ang 2016년 3월 25일
댓글: James Ang 2016년 4월 21일
Hi,
I've a function in GUIDE that goes:
% --- Executes on button press in plot2_push.
function plot2_push_Callback(hObject, eventdata, handles)
% hObject handle to plot2_push (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.time_sec2 = 1:size(handles.selected_file_table(:,'t'));
for i = 1:handles.count2;
plot(handles.axes2, handles.time_sec2, table2array(handles.selected_file_table(:,handles.selected_varlist2.String(i))),'LineWidth',2);
(handles.selected_file_table(:,handles.selected_varlist2.String(i))),'LineWidth',2);
axes(handles.axes2); hold on
end
grid(handles.axes2,'on');
handles.axes2.XMinorGrid = 'on';
handles.axes2.YMinorGrid = 'on';
hold off
set(handles.axes2,'Color',[0.87058824300766 0.921568632125854 0.980392158031464],...
'XColor',[0 0 0],'YColor',[0 0 0],'ZColor',[0 0 0]);
legend(handles.selected_varlist2.String,'Location','northoutside','Orientation','horizontal')
linkaxes([handles.axes1,handles.axes2],'x')
vertical_cursors1;
guidata(hObject, handles);
In the last line, I'm calling an external function to plot cursors (by Jiro) that goes like this:
function vertical_cursors1
set(gcf, ...
'WindowButtonDownFcn', @clickFcn, ...
'WindowButtonUpFcn', @unclickFcn);
% Set up cursor text
allLines = findobj(gcf, 'type', 'line');
hText = nan(1, length(allLines));
for id = 1:length(allLines)
hText(id) = text(NaN, NaN, '', ...
'Parent', get(allLines(id), 'Parent'), ...
'BackgroundColor', 'yellow', ...
'Color', get(allLines(id), 'Color'));
end
% Set up cursor lines
allAxes = findobj(gcf, 'Type', 'axes');
hCur = nan(1, length(allAxes));
for id = 1:length(allAxes)
hCur(id) = line([NaN NaN], ylim(allAxes(id)), ... %%hCur are cursor lines
'Color', 'black', 'Parent', allAxes(id));
end
function clickFcn(varargin)
% Initiate cursor if clicked anywhere but the figure
if strcmpi(get(gco, 'type'), 'figure')
set(hCur, 'XData', [NaN NaN]); % <-- EDIT
set(hText, 'Position', [NaN NaN]); % <-- EDIT
else
set(gcf, 'WindowButtonMotionFcn', @dragFcn)
[b, curtime1] = dragFcn();
disp(b); disp(curtime1);
end
end
function [yall,xtime] = dragFcn(varargin)
% Get mouse location
pt = get(gca, 'CurrentPoint');
% Update cursor line position
xtime = pt(1);
set(hCur, 'XData', [pt(1), pt(1)]);
% Update cursor text
yall = zeros(1,length(allLines));
for idx = 1:length(allLines) % allLines refering to each line
xdata = get(allLines(idx), 'XData'); % the x points for line #idx
ydata = get(allLines(idx), 'YData'); % the y points for line #idx
if pt(1) >= xdata(1) && pt(1) <= xdata(end)
y = interp1(xdata, ydata, pt(1));
% disp(y)
set(hText(idx), 'Position', [pt(1), y], ...
'String', sprintf('(%0.2f, %0.2f)', pt(1), y));
yall(:,idx) = y;
else
set(hText(idx), 'Position', [NaN NaN]);
end
end
end
function unclickFcn(varargin)
set(gcf, 'WindowButtonMotionFcn', '');
end
end
I've tried to pass data of 'yall' to the main GUI function but to no avail. Can someone help me?
Thanks.
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2016년 4월 11일
James - how do you expect to use the yall data? Will you be collecting it an array? At which point will you then use this data? You can try saving it to the handles structure which would be accessible by the other callbacks. Please provide more context.
James Ang
James Ang 2016년 4월 21일
Hi Geoff, sorry for the late reply.. (thought MW will send me an email if there're replies).
Anyways, I plan to display the 'yall' values on a table.
It's working now though, this is what I did (though I don't fully understand how data is passed between functions. I'll ask in another thread)
function dragFcn(hObject, eventdata, handles)
% Get mouse location
handles.pt = get(gca, 'CurrentPoint');
handles.pt(:,1) = round(handles.pt(:,1));
% Update cursor line position - moving line across x position
set(handles.hCur, 'XData', [handles.pt(1), handles.pt(1)]);
% Update cursor text
for idx = 1:length(handles.allLines)
xdata = get(handles.allLines(idx), 'XData');
ydata = get(handles.allLines(idx), 'YData');
if handles.pt(1) >= xdata(1) && handles.pt(1) <= xdata(end)
y = interp1(xdata, ydata, handles.pt(1));
set(handles.hText(idx), 'Position', [handles.pt(1), y], ...
'String', sprintf('(%0.0f, %0.2f)', handles.pt(1), y));
handles.yall(idx,:) = y;
else
set(handles.hText(idx), 'Position', [NaN NaN]);
end
end
handles.diff_table.Data(:, handles.cursornumber) = handles.yall;
if handles.cursornumber >= 2
handles.diff_table.Data(:,3) = handles.diff_table.Data(:,2) - handles.diff_table.Data(:,1);
else
end
guidata(hObject, handles);

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by