How can I select multiple points using the Data Cursor and export the coordinates to the MATLAB workspace?

511 views (last 30 days)
I would like to select multiple points in my figure using the Data Cursor, and export the coordinates of these points to the MATLAB workspace.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 9 Mar 2021
Edited: MathWorks Support Team on 9 Mar 2021
The following example illustrates how you can create a structure array which contains the coordinates of manually selected data points in a 2-D plot.
1. Execute the following at the command prompt:
2. Click the Data Cursor button on the toolbar of the generated figure.
3. Click any point of your choice on the line in the figure.
4. While pressing the Alt key, repeat step 3 as many times as you like until you have selected your desired set of points.
5. Right-click (or control-click if you are on a Mac) anywhere on the figure, and select the 'Export Cursor Data to Workspace...' option from the context menu.
6. Accept the default variable name, "cursor_info", and click "OK".
7. Type "cursor_info.Position" at the MATLAB command prompt and hit "Enter".
The structure array "cursor_info" contains all information regarding selection sequence, x- and y-coordinates of the points selected. Note that for 3-D figures, the selection sequence value is replaced by the z-coordinate value for the points selected.
Also note that the "cursor_info" varable will not appear in the Workspace browser until you complete step 7 above.
It is possible to obtain the data cursor information in the MATLAB workspace programmatically. The following example and the documentation link illustrates how to use the datacursormode function to obtain data cursor information programmatically:
fig = figure;
z = peaks;
dcm_obj = datacursormode(fig);
Now, click on the line to place datatip before executing the next line of code.
c_info = getCursorInfo(dcm_obj)
Malahat Mehraban
Malahat Mehraban on 15 Jun 2021
I would like to extract the indices of the selected point from this structure. How can I save the "DataIndex" matrix that is in the cursor_info?

Sign in to comment.

More Answers (1)

DP on 20 Jan 2016
Edited: DP on 20 Jan 2016
The following code enables multiple data points to be selected by the data cursor and to be exported to the workspace as many times as necessary to allow for curve fitting.
for k = 1:length(file)
% Load an image into Matlab
pic = imread(fullfile(file{k}));
% Define the number of times you want to repeat the process
% e.g. if there are multiple lines / shapes that you need to fit to
num_input = input(sprintf('Number of lines / shapes to detect? '));
clearvars c_info
for j = 1:num_input
% select n number of data points using the data cursor - in this case 15
for i = 1:15
dcm_obj = datacursormode(1);
c_info{i,j} = getCursorInfo(dcm_obj);
% Export the positions obtained by the data cursor to the workspace
for j = 1:num_input
for i = 1:size(c_info,1)
positions{i,j} = c_info{i,j}.Position;
for i = 1:num_input
% some code that fits your spline function to the data
close all

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!