catch line using callback
1 次查看(过去 30 天)
显示 更早的评论

mycallback = @(varargin) disp("yeah? what do ya want?");
plot(1:5,rand(1,5),buttondownfcn = mycallback)
but I want to capture the line that touches
i use this code to plot line
legend_lines(i)=plot(Ax_Eq,XDates,TE_strum(:,i),'Color',col(i,:));
after i want to using line capture changing color
legend_lines(line_callback).Color=[0 0 1]
It's possibile to do it?
0 个评论
采纳的回答
Benjamin Kraus
2025-6-6
编辑:Benjamin Kraus
2025-6-6
The function that is called by the ButtonDownFcn has the line that you clicked on as an input argument.
mycallback = @(o,~) set(o,'Color',[0 0 1]);
plot(1:5,rand(5,5),buttondownfcn = mycallback)
In the code above, o is a variable that stores the Line object that you clicked on. You can use set to set any properties on that object.
5 个评论
Benjamin Kraus
2025-6-6
编辑:Benjamin Kraus
2025-6-6
The example you and I provided both leverage an in-line anonymous function. Such a function is limited to only executing a single expression. If you want to run multiple commands, you need to switch to a regular function, which means you need to write your callback function in an M-file. For example, the following code will create 5 line objects and add a callback function that will make the line you click bold (i.e. increase the LineWidth) and set the LineWidth of the remaining objects back to 0.5.
function mycallback(o,~)
% Use the object to get to the axes.
ax = o.Parent;
% Use the axes to get to the other line objects.
allLines = ax.Children;
% Set the LineWidth on *all* the lines to 0.5.
set(allLines, 'LineWidth', 0.5);
% Set the LineWidth on just the clicked line to 3.
o.LineWidth = 3;
end
plot(1:5,rand(5,5),buttondownfcn = @mycallback)
You can't copy and paste that code into the command window and run it, because you can't define functions like that in the command window. You have two choices:
- Define a file called "mycallback.m" that has just the mycallback function (not the last line of code that calls plot). In this case, "mycallback.m" create a function with the name mycallback that can be called from any other script or function, as well as the command line. Then you can run the call to plot in the command window and reference mycallback using a function handle (@mycallback).
- Define a file with any name (such as testscript.m), with all the code above, then run the entire file as a script. In this case, mycallback is called a "local function" and is only available to code that is located in the same file (within testscript.m).
Benjamin Kraus
2025-6-6
编辑:Benjamin Kraus
2025-6-6
For the specific example above, with a little creativity, you can replicate the behavior using an in-line anonymous function. However, this usually doesn't work. For most examples of even mildly complicated callback functions, you will need to define the callback function in a separate file (as I showed in my previous comment).
Warning: The example below uses some tricks that make the code more complicated than necessary and thus harder to read and understand. Generally, this is a losing battle, and you are better off writing this as a function in an M-file.
However, because I can't resist:
mycallback = @(o,~) set([o.Parent.Children; o],{'LineWidth'},num2cell([0.5*ones(numel(o.Parent.Children),1); 3]));
plot(1:5,rand(5,5),buttondownfcn = mycallback)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!