Clear Filters
Clear Filters

assign a variable using a position changed function?

3 views (last 30 days)
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.

Accepted Answer

Stephen23
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:
I would recommend that you use guidata or nested functions (sadly these do not work with GUIDE).
[SL: fixed typo MATOLAB -> MATLAB.]
  3 Comments
Stephen23
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:
ryan muddiman
ryan muddiman on 18 May 2018
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

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!