jonnyd42
jonnyd42

Reputation: 500

Adding specific point info in MATLAB

I'm plotting the magnitude and phase response of a notch filter I designed and I need to mark the cutoff frequencies as well as the notch frequency point. I'd like to show the same information that shows up if you click on a plot. Is there a way to force MATLAB to show boxes like the one that shows up if you click on a point?

I have attached an image showing what I mean. I'd like to get a box like that at specific frequency points that I choose. enter image description here

Upvotes: 0

Views: 429

Answers (2)

Suever
Suever

Reputation: 65430

This can actually be done a little more reliably and robustly using some undocumented features of the data cursor (info). First we will get the figure's underlying datacursormode and then use that to add and move datatips (like the one in your figure).

fig = figure();

% Plot some fake data for now
xdata = linspace(0, 2*pi, 100);
ydata = sin(xdata);

hLine = plot(xdata, ydata);

% Get the datacursormode of the current figure and enable it
cursorMode = datacursormode(gcf);
set(cursorMode, 'enable','on')

Now that we have the datacursormode object, we can use it to add some new datatips.

datatip = cursorMode.createDatatip(handle(hLine));
% X,Y position at which to place the datatip
set(datatip, 'Position', [xdata(50), ydata(50)]);

enter image description here

We can move this datatip again if we want.

set(datatip, 'Position', [xdata(1), ydata(1)]);

The really nice thing, is that using the cursorMode object we can also create more datatips

datatip2 = cursorMode.createDatatip(handle(hLine));
set(datatip2, 'Position', [xdata(75), ydata(75)])

enter image description here

By combining this information with your own plots and data, you should be able to automatically place the datatips that you need reliably.

Upvotes: 0

shamalaia
shamalaia

Reputation: 2347

Here an example I built for an exponential function:

clear all

figure
f = plot(exp(1:10));

datacursormode on

% get the handle of the data cursor 
hdc = get(gcf,'WindowButtonDownFcn');
dcm = hdc{3}{2};


props.Position = [min(exp(1:10)) log(min(f)) 1];

dcm.createDatatip(f,props);

But you have to generalize the definition of the coordiates for the cursor to your case.

Upvotes: 1

Related Questions