Passing data from External function to main GUIDE function

1 view (last 30 days)
James Ang
James Ang on 25 Mar 2016
Commented: James Ang on 21 Apr 2016
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 Comments
James Ang
James Ang on 21 Apr 2016
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);

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!