Passing output from a ButtonDownFcn callback function
Hello,
I am struggling with figuring out how to pass variables from a callback function (using ButtonDownFcn) to my main script.
Below is a simple example where I am trying to modify the example script from:
https://nl.mathworks.com/help/images/measure-distances-in-images.html
I simplified it below to just make the first function work and have two examples below:
– the original line (LINE A below), which doesn’t pass my variable back and doesn’t seem to allow me to define further output variables. Adding variables (double) to hIm also doesn’t work (I guess because it is an image object?)
– the simple line (LINE B below) works to pass the output variable, but is not an option because it doesn’t use the ButtonDownFcn anymore (which I need later).
clear all
close all
clc
im = imread(‘SENIAM_EMGEP1.jpeg’);
% Gather data about the image, such as its size, and store the data in a structure that you can pass to callback functions.
sz = size(im);
myData.Units = ‘pixels’;
myData.MaxValue = hypot(sz(1),sz(2));
myData.Colormap = hot;
myData.ScaleFactor = 1;
% Display the image in an axes.
hIm = imshow(im);
% Specify a callback function for the ButtonDownFcn callback on the image. Pass the myData structure to the callback function. This callback function creates the line objects and starts drawing the ROIs.
hIm.ButtonDownFcn = @(~,~) startDrawing(hIm.Parent,myData); %% LINE A
myData = startDrawing(hIm.Parent,myData); %% LINE B
function myData = startDrawing(hAx,myData)
myData.test = 2;
% Create a line ROI object. Specify the initial color of the line and
% store the |myData| structure in the |UserData| property of the ROI.
h = images.roi.Line(‘Color’,[0, 0, 0.5625],’UserData’,myData);
end
I have had a similar issue with callback functions and thought it was time to seek help.
The answer provided here (https://nl.mathworks.com/matlabcentral/answers/563102-passing-a-variable-from-a-call-back-function-matlab-2020a) doesn’t allow me to figure it out and I want to avoid using global variables.
Thanks for any advice.Hello,
I am struggling with figuring out how to pass variables from a callback function (using ButtonDownFcn) to my main script.
Below is a simple example where I am trying to modify the example script from:
https://nl.mathworks.com/help/images/measure-distances-in-images.html
I simplified it below to just make the first function work and have two examples below:
– the original line (LINE A below), which doesn’t pass my variable back and doesn’t seem to allow me to define further output variables. Adding variables (double) to hIm also doesn’t work (I guess because it is an image object?)
– the simple line (LINE B below) works to pass the output variable, but is not an option because it doesn’t use the ButtonDownFcn anymore (which I need later).
clear all
close all
clc
im = imread(‘SENIAM_EMGEP1.jpeg’);
% Gather data about the image, such as its size, and store the data in a structure that you can pass to callback functions.
sz = size(im);
myData.Units = ‘pixels’;
myData.MaxValue = hypot(sz(1),sz(2));
myData.Colormap = hot;
myData.ScaleFactor = 1;
% Display the image in an axes.
hIm = imshow(im);
% Specify a callback function for the ButtonDownFcn callback on the image. Pass the myData structure to the callback function. This callback function creates the line objects and starts drawing the ROIs.
hIm.ButtonDownFcn = @(~,~) startDrawing(hIm.Parent,myData); %% LINE A
myData = startDrawing(hIm.Parent,myData); %% LINE B
function myData = startDrawing(hAx,myData)
myData.test = 2;
% Create a line ROI object. Specify the initial color of the line and
% store the |myData| structure in the |UserData| property of the ROI.
h = images.roi.Line(‘Color’,[0, 0, 0.5625],’UserData’,myData);
end
I have had a similar issue with callback functions and thought it was time to seek help.
The answer provided here (https://nl.mathworks.com/matlabcentral/answers/563102-passing-a-variable-from-a-call-back-function-matlab-2020a) doesn’t allow me to figure it out and I want to avoid using global variables.
Thanks for any advice. Hello,
I am struggling with figuring out how to pass variables from a callback function (using ButtonDownFcn) to my main script.
Below is a simple example where I am trying to modify the example script from:
https://nl.mathworks.com/help/images/measure-distances-in-images.html
I simplified it below to just make the first function work and have two examples below:
– the original line (LINE A below), which doesn’t pass my variable back and doesn’t seem to allow me to define further output variables. Adding variables (double) to hIm also doesn’t work (I guess because it is an image object?)
– the simple line (LINE B below) works to pass the output variable, but is not an option because it doesn’t use the ButtonDownFcn anymore (which I need later).
clear all
close all
clc
im = imread(‘SENIAM_EMGEP1.jpeg’);
% Gather data about the image, such as its size, and store the data in a structure that you can pass to callback functions.
sz = size(im);
myData.Units = ‘pixels’;
myData.MaxValue = hypot(sz(1),sz(2));
myData.Colormap = hot;
myData.ScaleFactor = 1;
% Display the image in an axes.
hIm = imshow(im);
% Specify a callback function for the ButtonDownFcn callback on the image. Pass the myData structure to the callback function. This callback function creates the line objects and starts drawing the ROIs.
hIm.ButtonDownFcn = @(~,~) startDrawing(hIm.Parent,myData); %% LINE A
myData = startDrawing(hIm.Parent,myData); %% LINE B
function myData = startDrawing(hAx,myData)
myData.test = 2;
% Create a line ROI object. Specify the initial color of the line and
% store the |myData| structure in the |UserData| property of the ROI.
h = images.roi.Line(‘Color’,[0, 0, 0.5625],’UserData’,myData);
end
I have had a similar issue with callback functions and thought it was time to seek help.
The answer provided here (https://nl.mathworks.com/matlabcentral/answers/563102-passing-a-variable-from-a-call-back-function-matlab-2020a) doesn’t allow me to figure it out and I want to avoid using global variables.
Thanks for any advice. callback, passing variables MATLAB Answers — New Questions