Sharing data between callback functions

Hi, so in one callback function, I have the code,
file = uigetfile('*.jpg');
I would like to acess this file from a different callback function. However when I run the second callback, i get the error, "unrecognized function"
How do I allow the file to be acessed from other callback functions?

9 Comments

Adam
Adam on 10 Feb 2020
Edited: Adam on 10 Feb 2020
By the way, if you'd typed what you wrote as the title of this question into the Matlab help, this would have been one of the first 3 links it takes you to - it should always be your first port of call as it is often much faster than asking in the forum and you learn more often too!
I have attempted these techniques, however I am still unable to access the image file from another callback function
I have attempted these techniques
Then show us what you tried, so we can tell you how to fix it. Also, state if this is using App Designer or GUIDE.
I am using App Designer. I have tried to create a private property where i have written the code,
properties (Access = private)
file; % Description
end
and then called the file later with the code,
faceDetector = vision.CascadeObjectDetector;
bboxes = step(faceDetector, app.file);
This line gives me the error,
Error using vision.CascadeObjectDetector/validateInputsImpl (line 339)
Expected input number 2 to be one of these types:
uint8, uint16, double, single, int16
Since it wants a numeric variable, are you sure you don't need to read the image instead of providing a file name?
Yes, I think I do want to read the image.
Then it probably makes sense to imread. Whether you want to set the image or the path as property is up to you.
Don't forget to validate the user input (so check if the user actually did select a file).
The issue with imread is that I need to access the file that was chosen in another callback function. So I am not sure how to get the file name from the other callback function.
Thanks to everyone who helped.

Sign in to comment.

 Accepted Answer

"The issue with imread is that I need to access the file that was chosen in another callback function. So I am not sure how to get the file name from the other callback function."
You already have done it! The issue with your original error, as pointed out by Rik is that you never read the image, just passed the filename instead of the image to the face detector.
So instead of:
bboxes = step(faceDetector, app.file);
simply:
faceimage = imread(app.file); %actually read the image
bboxes = step(faceDetector, faceimage);

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!