Clear Filters
Clear Filters

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

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

Accepted Answer

Adam
Adam on 19 Jul 2016
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 Comment
Nicholas Trunfio
Nicholas Trunfio on 19 Jul 2016
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

Sign in to comment.

More Answers (0)

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!