assign a variable using a position changed function?
2 views (last 30 days)
Show older comments
I am trying to plot a line segment of an image array graphically using horizontal and vertical lines as cursors using imline. To continuously update the line plot I would need to store the position of the line every time it changes position. However, using a position change function, assignin('base','pos',getposition(h) doesnt work and I cant seem to assign a variable within the addnewpositionCallback.
handles.Line = imline (handles.axes1, [50 50], [0 400]);
api = iptgetapi(handles.Line);
fcn = @(pos) [min(pos(:,1)) pos(1,2); min(pos(:,1)) pos(2,2)];
api.setDragConstraintFcn(fcn);
fcn = makeConstrainToRectFcn('imline',get(gca,'XLim'),get(gca,'YLim'));
setPositionConstraintFcn(handles.Line,fcn);
addNewPositionCallback(handles.Line,@(p) assignin('base','x',getPosition(handles.Line)));
addNewPositionCallback(handles.Line,@(j) plot((cropdata{1,roi}(:,x(1),p)),'Parent','handles.axes2'));
The last line returns an error as x is apparently undefined even though I can see it updating in the workspace when I move the line.
Any help is appreiated.
0 Comments
Accepted Answer
Stephen23
on 17 May 2018
Edited: Steven Lord
on 17 May 2018
Forget about what you can see in the base workspace. It does not help you, because the workspace where you try to use that values is NOT the base workspace. It is the workspace of that particular callback. Every function, including callback functions, have their own independent workspace, as explained in the MATLAB documentation:
There really is no point in spamming data into the base workspace using assignin, evalin, etc, because then these are just more difficult to access (slower, buggier, etc). The values you want is obtained in one callback, so all you need to do is transfer that value to whatever other callback you have where you want to use it. How to move values between callbacks is explained in the MATLAB documentation:
[SL: fixed typo MATOLAB -> MATLAB.]
3 Comments
Stephen23
on 18 May 2018
Edited: Stephen23
on 18 May 2018
As far as I can tell you need to add one callback. Make sure that you have stored any data that you require in handles, and then you can access it later in the callback (untested):
handles.Line = ...
handles.roi = ...
addNewPositionCallback(handles.Line,@myfun);
guidata(...,handles) % make sure that you store handles!
...
function myfun(p)
handles = guidata(gcf);
x = getPosition(handles.Line);
plot((cropdata{1,roi}(:,x(1),p)),'Parent',handles.axes2)
end
See also:
More Answers (0)
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!