Clear Filters
Clear Filters

Select a multipe lines in a plot and store each lines XData and YData in an array

3 views (last 30 days)
I'm plotting a lot of map data and I want to be able to select multiple lines to create a route by storing the X and Y co-ordinates.
I have found the following function that allows a user to select each line and then make it thicker, etc.
I've tried to modify the second function by adding
Var = num2str(h(1).XData)
but to no avail.
Does anyone know how I can implement this?
function smallTest
axes('NextPlot', 'add')
H(1) = plot(1:10, rand(1, 10), 'r');
H(2) = plot(1:10, rand(1, 10), 'b');
set(H, 'ButtonDownFcn', {@LineSelected, H})
function LineSelected(ObjectH, EventData, H)
set(ObjectH, 'LineWidth', 2.5);
set(H(H ~= ObjectH), 'LineWidth', 0.5);

Answers (1)

KSSV
KSSV on 24 Nov 2016
function smallTest
axes('NextPlot', 'add')
H(1) = plot(1:10, rand(1, 10), 'r');
H(2) = plot(1:10, rand(1, 10), 'b');
set(H, 'ButtonDownFcn', {@LineSelected, H})
end
function LineSelected(ObjectH, EventData, H)
set(ObjectH, 'LineWidth', 2.5)
set(H(H ~= ObjectH), 'LineWidth', 0.5) ;
% Get x and y data of the highlighted lines
x = ObjectH.XData
y = ObjectH.YData
end
  4 Comments
Adam
Adam on 24 Nov 2016
Use a nested function for the callback and define the variables you want to store into in the main function.
Trang Luu
Trang Luu on 23 Nov 2019
Edited: Trang Luu on 23 Nov 2019
Sorry can you please explain a bit more?
I was able to add the following lines of code to save my X and Y values to my workspace, but every time I select another line on my plot, the variable would update. I would like the variable to just be added to instead of constantly replaced.
function LineSelected(ObjectH, EventData, H)
set(ObjectH, 'LineWidth', 2.5)
set(H(H ~= ObjectH), 'LineWidth', 0.5) ;
% Get x and y data of the highlighted lines
x = ObjectH.XData
y = ObjectH.YData
assignin('base','XinWorkSpace',x);
assignin('base','YinWorkSpace',y);
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!