Workspace parameter values UPDATE while using "Mouse Call-back" function

Hello everyone!
I am struggling with the next problem, and I hope some of you can help me! :)
I have here a plot, having already defined the X and Y values as 1x10 double arrays, and a computation for the sum of the first 6 elements of the Y-axis array (just an example).
The Mouse_Callback function allows me to change the Y-axis values in real time, as you can see in the code attached below, and always get the new point coordinate in the command window.
Problem: Even though the function gives me the new coordinates of the YData, the Sum_Y is not re-calculated. It is very important to calculate the Sum_Y outside the function (in the main), because I would like to keep the function strictly for real-time Y-axis modification of the plot. I have to specify that the new Y-axis values appear only in the Command Window, without being updated in the Workspace (therefore the problem with the Sum_Y recalculation).
Question: How can I modify the Y-axis with drag&drop, like the function does, and get the values updated immediately in the Workspace, so I can immediately calculate Sum_Y and keep it updated everytime I change a point on the plot?
Thank you very much in advance! Any piece of advice would be very much appreciated!!!
close all
clear
clc
Graph_pos = [2.5 7 45 10];
x = [1 2 3 4 5 6 7 8 9 10 ];
y = [0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 ];
%% Plotting and Manually updating the values
fig_1 = figure('Units','centimeters','Position',Graph_pos);
clearvars Graph_pos
plot(x,y,'k-o','MarkerfaceColor','k','buttondownfcn',{@Mouse_Callback,'down'});
[x_coord,y_coord] = Mouse_Callback(gcf,gca,0);
Sum_Y = y(1) + y(2) + y(3) + y(4) + y(5) + y(6); % this sum is just an example, I need to get it updated in real time
% Callback function for each point
function [x_coord, y_coord] = Mouse_Callback(hObj,~,action)
persistent curobj xdata ydata ind
pos = get(gca,'CurrentPoint'); % gets the exact position of the point, when we click on it
switch action
case 'down' % down = click without release
curobj = hObj; % gives the plot properties (Color, Line settings, ..., XData, YData, ZData)
xdata = get(hObj,'XData'); % xdata takes the X-axis coordinates from the plot properties
ydata = get(hObj,'YData'); % ydata takes the Y-axis coordinates from the plot properties
[~,ind] = min(sum((xdata-pos(1)).^2+(ydata-pos(3)).^2,1)); % ind is the index of the axis value (1st, 2nd, ..., 10th)
set(gcf,...
'WindowButtonMotionFcn', {@Mouse_Callback,'move'},... % allows to move the point after pressing it
'WindowButtonUpFcn', {@Mouse_Callback,'up'}); % allows to release and keep the updated position of the point
case 'move' % drag the click without release
% vertical move
ydata(ind) = pos(3); % the selected point takes the new Y-axis coordinates value
set(curobj,'ydata',ydata) % the YData values get updated, and the point moves on the graph
case 'up' % release the click
set(gcf,...
'WindowButtonMotionFcn', '',...
'WindowButtonUpFcn', '');
disp(ydata) % Real time Y coordinates values
end
dataObjs = findobj(gcf,'-property','XData');
x_coord = dataObjs(1).XData;
dataObjs = findobj(gcf,'-property','YData');
y_coord = dataObjs(1).YData;
end

 Accepted Answer

There are two kinds of callback functions.
By far the most common kind of callback function cannot return values.
A small portion of callback functions are used as "filter" functions, such as checking to see if a proposed pan is within acceptable limits: those must return values.
KeyPressFcn callbacks are part of the large majority that cannot return values.
plot(x,y,'k-o','MarkerfaceColor','k','buttondownfcn',{@Mouse_Callback,'down'});
The script that configured the buttondownfcn callback is no long working by the time the callback is invoked. Those kind of callbacks cannot be invoked until the graphics queue is given a chance to execute, when drawnow() or pause() is invoked or waitfor() or uiwait(), or the function returns to the keyboard (or keyboard() is invoked).
You might be able to work something out using a listener set to detect post-set for the ydata, or you could loop with a waitfor() of a change the ydata property. Or you could have the callback invoke whatever action is needed.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!