Use data tip custom script in app designer

31 views (last 30 days)
Benoit Igne
Benoit Igne on 25 Mar 2021
Answered: Dinesh on 10 Mar 2023
Hello,
I am using MATLAB 2020a. I am building an app with multiple UIAxes in a figure. What I would like to do is that when I click on a point from one of the UIAxes, a new figure opens with a custom plot showing some properties of the data point I selected. I have not been able to do that with the app designer just yet.
Is there a way to do what I just described?
Since I have not be able to do this yet, I am working on a way around. The app designer data tip on a plot allows one to use a custom script to trigger an action when clicking on a data point (see picture below). MATLAB does provide the option to load a custom script. This custom script gives me an opportunity to do what I described above (open a new plot showing some properties of the point I selected) but I am stuck.
  1. The custom script is saved in the path (not in the app), thus does not have access to the data from the app. Is there a way to pass data from the app to the script? Or to execute this custom script directly from the app?
  2. Is there a way to tell the app which custom data tip script should be loaded for a particular UIAxe? This question is because I have several UIAxes on my app and each data tip custom script would show different properties.
Thank you for the help.

Answers (1)

Dinesh
Dinesh on 10 Mar 2023
Hi Benoit.
One way to achieve this is to use the "Tag" property of each UIAxes to specify which custom script should be executed when a point in that UIAxes is clicked. I'm assuming that the relationship between different UIAxes and custom scripts to execute is one to one.
To pass data directly to the custom script file, command line arguments can be used.
Here are some steps that you can follow:
  1. Set the "Tag" property of each UIAxes to a unique identifier that corresponds to the name of the custom script file that should be executed. For example, if you have a UIAxes with a scatter plot and you want to execute a custom script called "customScript1.m" when a point in that UIAxes is clicked, you can set the "Tag" property of that UIAxes to "customScript1".
  2. In the "DataCursorClickFcn" callback function for each UIAxes, use the "Tag" property to determine which custom script should be executed. You can do this using a switch statement or an if-else statement. For example:
function UIAxesDataCursorClick(app, event)
% Get the unique tag that you defined for each UIAxes
uiAxesTag = event.Target.Tag;
% Get the data associated with the clicked point
x = event.Position(1); % just an example of data
y = event.Position(2); % just an example of data
% Execute the custom script for this UIAxes
switch uiAxesTag
case 'customScript1'
customScriptPath = which('customScript1.m');
command = sprintf('customScript1(%f, %f)', x, y); % passing data to the script (the script file is a function)
system(sprintf('matlab -r "%s"', command)); % since it's a different file in the path, another instance of matlab is used.
case 'customScript2'
customScriptPath = which('customScript2.m');
command = sprintf('customScript2(%f, %f)', x, y);
system(sprintf('matlab -r "%s"', command));
% More cases can be added for different UIAxes
end
end
The "system" function is used to call another instance of MATLAB since the custom script is a different file in the path altogether and not executed along with the app.
The "sprintf" function is used to format the command line argument as a string.
In each custom script, add input arguments to receive the data from the app when a particular point in the UI Axes is clicked upon.
function customScript1(x, y)
% Plot the custom figure using the data
% More code
end
By doing this, you no longer have to manually choose the script from the path along with being able to pass data to the script.

Categories

Find more on Develop Apps Using App Designer 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!