フィルターのクリア

Adding the variable name onto a plot cursor

14 ビュー (過去 30 日間)
S. Moore
S. Moore 2018 年 7 月 26 日
コメント済み: Chandrashekar D S 2020 年 7 月 6 日
I regularly plot several variables. This results in 12 lines on a plot. For example with the x-axis being time, several channels of voltages are plotted. I can add a legend, but it's really hard to discern the different shades of blue or red or green to identify which curve is which. I want to add something in the data cursor callback function that will display the name of the selected line, so I can correlate the selected line with the legend box.
  1 件のコメント
Chandrashekar D S
Chandrashekar D S 2020 年 7 月 6 日
did you find any solution for this?

サインインしてコメントする。

回答 (2 件)

Steven Lord
Steven Lord 2018 年 7 月 26 日
I would consider using color, line style, and/or marker to distinguish the twelve lines. Using this example as inspiration:
% Make an axes
ax = axes;
% Change the ColorOrder and LineStyleOrder for the axes
% 3 values for ColorOrder
ax.ColorOrder = [1 0 0; % red
0 1 0; % green
0 0 1]; % blue
% times 4 values for LineStyleOrder gives 12 combinations
ax.LineStyleOrder = {'-o', '--*', ':', '-d'};
% Normally, calling plot resets axes properties
% Make it so it doesn't.
ax.NextPlot = 'replacechildren';
% Plot the 12 lines and show the legend.
plot(magic(12))
legend show
Each line differs from the others in line style, color, and/or marker.
  1 件のコメント
S. Moore
S. Moore 2018 年 7 月 26 日
編集済み: S. Moore 2018 年 7 月 26 日
Since I regularly use data cursors - sometimes with custom data display, like 6 decimal points - using a callback function, it seems there should be a way to display the variable name in which the selected point belongs. The callback function uses the following to get the [X,Y] position of the data point:
pos = get(event_obj,'Position');
All I need is something similar to:
variablename = get(event_obj,'????');
where '????' is the one thing I need to know.

サインインしてコメントする。


Peter Meglis
Peter Meglis 2018 年 7 月 26 日
編集済み: Peter Meglis 2018 年 7 月 26 日
Take a look at the example halfway down this doc page: https://www.mathworks.com/help/matlab/ref/datacursormode.html
And this one for the get function: https://www.mathworks.com/help/matlab/ref/get.html
If you use datacursormode on your figure, you can get the cursor info, from that you can get the DisplayName (label) from the line you are focused on.
Something like:
fig = figure;
...
dcm_obj = datacursormode(fig);
...
c_info = getCursorInfo(dcm_obj);
disp(get(c_info.Target, 'DisplayName'));
Hope this helps!
  3 件のコメント
Peter Meglis
Peter Meglis 2018 年 7 月 27 日
Take a look at this doc page for some examples: https://www.mathworks.com/help/matlab/creating_plots/callback-definition.html
If you supply the 'ButtonDownFcn' parameter with a function handle (@nameOfFunction) to your plot function, this callback will be called any time you click on one of the lines. The function must accept at least two arguments: "the handle of the object whose callback is executing" and "the event data structure" which can be empty like below.
...
plot(..., 'ButtonDownFcn', @displayNameOfLine);
...
function displayNameOfLine(src, ~)
disp(get(src, 'DisplayName'))
end
From here you can do whatever you want in terms of displaying the DisplayName and including the datacursormode.
Hope this helps!
Victor Braescu
Victor Braescu 2020 年 4 月 2 日
編集済み: Victor Braescu 2020 年 4 月 2 日
Thanks Peter! This helped with my problem where I had 500 different lines on my graph and wanted to see the legend name of each indivdually.
I edited it a bit to display the name directly on the graph and then delete the name after a second. I used text instead of disp to do this. Hopfully this helps anyone that is interested!
function displayNameOfLine(src, ~)
name = text(0.3,0.8,get(src, 'DisplayName'),'FontSize',50,'units','normalized');
pause(1);
delete (name);
end

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by