assign a variable using a position changed function?

3 次查看(过去 30 天)
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.

采纳的回答

Stephen23
Stephen23 2018-5-17
编辑:Steven Lord 2018-5-17
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:
I would recommend that you use guidata or nested functions (sadly these do not work with GUIDE).
[SL: fixed typo MATOLAB -> MATLAB.]
  3 个评论
Stephen23
Stephen23 2018-5-18
编辑:Stephen23 2018-5-18
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:
ryan muddiman
ryan muddiman 2018-5-18
Ok I got it to work thanks to:
function myfun(p)
handles = guidata(gcf);
x = getPosition(handles.Line);
plot((cropdata{1,roi}(:,x(1),p)),'Parent',handles.axes2)
end
Thanks Stephen

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by