assign a variable using a position changed function?
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
ryan muddiman
le 17 Mai 2018
Modifié(e) : ryan muddiman
le 29 Juin 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.
0 commentaires
Réponse acceptée
Stephen23
le 17 Mai 2018
Modifié(e) : Steven Lord
le 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:
[SL: fixed typo MATOLAB -> MATLAB.]
3 commentaires
Stephen23
le 18 Mai 2018
Modifié(e) : Stephen23
le 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:
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!