Why is graphics object not being passed by reference in nested functions? (gobjects array loses values when function closes)

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

采纳的回答

Adam
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.
  1 个评论
Nicholas Trunfio
Nicholas Trunfio 2016-7-19
OK, that makes sense.
For anyone else with a similar issue, this is the change to fix it.
... Prior code reads the curSpec from an excel file and uses a file exchange file to set the plot colors depending on its size
% Don't use gobjects in the main code, create the array inside the function
handles.refRec = openRefSpecAnalysis(handles.curSpec, handles.plotCol);
function rect = openRefSpecAnalysis(curSpec, plotColors)
drawSpectra(refPlotAxes, curSpec); % Function works fine, not recreated here
rect = drawRect(refPlotAxes, plotColors, rect); % Function not working, recreated below
openRefAnalysisWind(plotColors); % Function works fine, not recreated here
end % Breakpoint 2 placed here
% Return the new array of rectangles from the function to the main gui
function rect = drawRect(curAxes, plotCol)
set(plCallWind, 'CurrentAxes', curAxes);
rect = gobjects(length(plotCol(:, 1)), 1); % Initialize the graphics placeholders here
for cnt = 1:length(plotCol(:, 1)) % And convert each element to a rectangle in the same function
rect(cnt, 1) = rectangle('Position', [0 0 1 1], 'FaceColor', plotCol(cnt, :), 'Visible', 'Off')
end
end % Breakpoint 1 placed here

请先登录,再进行评论。

更多回答(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