hi, John D'Errico helped me to code this:
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?

 採用された回答

Benjamin Kraus
Benjamin Kraus 2025 年 6 月 6 日
編集済み: Benjamin Kraus 2025 年 6 月 6 日

0 投票

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 件のコメント

shamal
shamal 2025 年 6 月 6 日
thanks you
but is it possible to execute a sequence of actions when I call the callback? example: I color all the other curves transparent and the one I touch blue
Benjamin Kraus
Benjamin Kraus 2025 年 6 月 6 日
Here is another example that will toggle the LineWidth between the default (0.5) and 2.5 when you click on the object. You can use something like this to indicates a line has been "selected" and then click on a line to either select or de-select the line.
mycallback = @(o,~) set(o,'LineWidth',3-o.LineWidth);
plot(1:5,rand(5,5),buttondownfcn = mycallback)
Once you are done clicking, you can find all the objects that are "selected" like this:
selected = findobj('Type','line','LineWidth',2.5)
shamal
shamal 2025 年 6 月 6 日
one last doubt: In selected I have all the objects that I want to make transparent but when I call the callback it has to do 2 functions: 1) Make the selected line thicker 2) make the objects in selected transparent How can I do both with the callback?
Benjamin Kraus
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:
  1. 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).
  2. 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
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 件)

カテゴリ

ヘルプ センター および File ExchangeMigrate GUIDE Apps についてさらに検索

質問済み:

2025 年 6 月 6 日

編集済み:

2025 年 6 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by