Filter löschen
Filter löschen

assign a variable using a position changed function?

3 Ansichten (letzte 30 Tage)
ryan muddiman
ryan muddiman am 17 Mai 2018
Bearbeitet: ryan muddiman am 29 Jun. 2019
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.

Akzeptierte Antwort

Stephen23
Stephen23 am 17 Mai 2018
Bearbeitet: Steven Lord am 17 Mai 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 Kommentare
Stephen23
Stephen23 am 18 Mai 2018
Bearbeitet: Stephen23 am 18 Mai 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 am 18 Mai 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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Object Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by