Why is graphics object not being passed by reference in nested functions? (gobjects array loses values when function closes)
2 次查看(过去 30 天)
显示 更早的评论
I'm trying to initialize an array of rectangles as my gui opens (all with their 'Visibility' set to 'Off').
First, create the array to hold the objects:
handles.refRec = gobjects(21, 1);
Then, call my function that draws the initial gui window:
... Prior code reads the curSpec from an excel file and uses a file exchange file to set the plot colors depending on its size
openRefSpecAnalysis(handles.curSpec, handles.plotCol, handles.refRec);
function openRefSpecAnalysis(curSpec, plotColors, rect)
drawSpectra(refPlotAxes, curSpec); % Function works fine, not recreated here
drawRect(refPlotAxes, plotColors, rect); % Function not working, recreated below
openRefAnalysisWind(plotColors); % Function works fine, not recreated here
end % Breakpoint 2 placed here
function drawRect(curAxes, plotCol, rect)
set(plCallWind, 'CurrentAxes', curAxes);
for cnt = 1:length(plotCol(:, 1))
rect(cnt, 1) = rectangle('Position', [0 0 1 1], 'FaceColor', plotCol(cnt, :), 'Visible', 'Off')
end
end % Breakpoint 1 placed here
When I hit breakpoint 1, rect is filled with 21 invisible rectangles. I use get(rect(1, 1)) at the command line to make sure it's a rectangle object with the correct position, visible properties. However, when I hit breakpoint 2, rect is now an array of root objects (just as when it is first created by gobjects). My understanding is that graphics objects are passed as handles (by reference?) and not as values, and so the modifications to rect in drawRect should be seen in openRefSpecAnalysis after drawRect executes.
0 个评论
采纳的回答
Adam
2016-7-19
The array of GraphicsPlaceholder objects is not passed by reference, it is the objects themselves that would be passed by reference - i.e. the individual graphics objects.
You replace those GraphicsPlaceholder objects with new instances of rectangles.
If you were passing in an array already filled with rectangles and editing these then they would be fine and those objects would update by reference, but replacing the objects themselves with new ones means this is not the case - you have different graphics objects in your inner function than you did in the outer one.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!