简体   繁体   中英

Matlab automating analysis of graphs using datacursormode

I have to analyse a lot of data, to do this I defined several classifiers of the data. I want to make something dat if I click with the data cursor on a point on the graph it stores the point and subtracts it from the next. So that I can find the peak to peak heigth of a sine. Like y2-y1.

I found the following code to extract the points. Only it's a function that is called every time and has no memory.

function out = getIndex(obj,event_obj,X,Y)
pos = event_obj.Position;
d1 = (X-pos(1)).^2 + (Y-pos(2)).^2;
[ignore index] = min(d1);
out = {sprintf('X: %f',pos(1)),...
sprintf('Y: %f',pos(2)),...
sprintf('Index: %d',index)};
% disp(pos(1))
% pos(1)
pos(2)
save pos.mat pos

In Matlab you can test with:

X = 1:10;
Y = rand(1,10);
plot(X, Y)

You can apply the above function to be used with the datacursormode using:

dcm = datacursormode(gcf);
set(dcm, 'UpdateFcn', @(x,y)getIndex(x,y,X,Y))

I recommend using another way in here. Instead of setting the callback for data cursor, set the callback directly for the plot. Extract the (X,Y) by querying the axes 'CurrentPoint'.

function so2()
figure();
a = axes();
x = -10:0.01:10;
sx = sin(x);
h = plot(x,sx);
set(h,'ButtonDownFcn',{@Click_CallBack a});

end

function Click_CallBack(h,e,a)
point = get(a,'CurrentPoint'); x = point(1);
y = point(4);
fprintf(1,'X,Y = %.2f,%.2f\\n',x,y);
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM