Graphics argument validation for function in parfor loop

I have a function that I'd like to run serially but also sometimes inside a parfor loop, with the ability to optionally plot output. Understandably, I can't plot or modify graphics within a parpool thread, but I find even having unused relevant argument validation that draws from matlab.graphics.axis.Axes class properties causes a parallel:threadpool:NonConcurrentLibrary error to be thrown during parallel execution. Attached is a code snippet demonstrating what I mean.
I suppose I have two questions. First, is this expected behaviour, given that args is not even invoked in the body of the function? Second, is there any way to retain automatic argument validation for an axis object that plays nicely with parfor, in cases where these arguments are used conditionally and never during parallel execution?
function ArgValidationParForTest()
pool = gcp();
fprintf('Parallel pool initialised with %d workers\n', pool.NumWorkers)
ntests = 10;
parfor i = 1:ntests
TestFunc()
end
end
function TestFunc(args)
arguments
args.?matlab.graphics.axis.Axes
end
fprintf('Called\n')
end

 Accepted Answer

If you use a process-based pool, it will work --
parpool('Processes')

More Answers (1)

Matt J
Matt J on 19 May 2026 at 17:48
Edited: Matt J on 19 May 2026 at 17:54
Second, is there any way to retain automatic argument validation for an axis object that plays nicely with parfor, in cases where these arguments are used conditionally and never during parallel execution?
You would probably have to hand off to a separate validation routine,
function TestFunc(varargin)
fprintf('Called\n')
args=handOff(varargin{:});
end
function args=handOff(varargin)
p = gcp('nocreate');
args=[];
if isempty(p) || ~isa(p,'parallel.ThreadPool')
args=validateGraphics(varargin{:});
elseif ~isempty(varargin)
error 'Handle graphics aren''t supported in threadpools'
end
end
function args=validateGraphics(args)
arguments
args.?matlab.graphics.axis.Axes
end
end

2 Comments

Thanks, I think this level of burying removes a lot of the benefits of argument validation for me (CLI completion, code clarity). But, it's nice to know such a solution is possible
You can recover some of the benefits of CLI by adding an explicit argument list, i.e., by copy-pasting the following auto-generated argument list into your TestFunc arguments block. This block knows only the names of the name/value parameters, and not any of the defaut values and type restrictions. So you would still need to use the handOff function to do the validation proper. However, it at least it will complete the parameter names for you.
disp(char( " args."+properties("matlab.graphics.axis.Axes") ))
args.CameraPosition args.CameraPositionMode args.CameraTarget args.CameraTargetMode args.CameraUpVector args.CameraUpVectorMode args.CameraViewAngle args.CameraViewAngleMode args.View args.Projection args.LabelFontSizeMultiplier args.AmbientLightColor args.DataAspectRatio args.DataAspectRatioMode args.PlotBoxAspectRatio args.PlotBoxAspectRatioMode args.FontName args.FontAngle args.FontWeight args.TickLabelInterpreter args.XLim args.XLimMode args.YLim args.YLimMode args.ZLim args.ZLimMode args.XLimitMethod args.YLimitMethod args.ZLimitMethod args.XDir args.YDir args.ZDir args.CLim args.CLimMode args.ALim args.ALimMode args.Layer args.TickLength args.GridLineWidth args.GridLineWidthMode args.MinorGridLineWidth args.MinorGridLineWidthMode args.GridLineStyle args.MinorGridLineStyle args.XAxisLocation args.XColor args.XColorMode args.XTick args.XTickMode args.XTickLabelRotation args.XTickLabelRotationMode args.XLabel args.XScale args.XTickLabel args.XTickLabelMode args.XMinorTick args.YAxisLocation args.YColor args.YColorMode args.YTick args.YTickMode args.YTickLabelRotation args.YTickLabelRotationMode args.YLabel args.YScale args.YTickLabel args.YTickLabelMode args.YMinorTick args.ZColor args.ZColorMode args.ZTick args.ZTickMode args.ZTickLabelRotation args.ZTickLabelRotationMode args.ZLabel args.ZScale args.ZTickLabel args.ZTickLabelMode args.ZMinorTick args.BoxStyle args.LineWidth args.Color args.ClippingStyle args.CurrentPoint args.ColorScale args.AlphaScale args.Title args.TitleHorizontalAlignment args.Subtitle args.GridColor args.GridColorMode args.MinorGridColor args.MinorGridColorMode args.GridAlpha args.GridAlphaMode args.MinorGridAlpha args.MinorGridAlphaMode args.XAxis args.ZAxis args.Box args.TickDir args.TickDirMode args.XGrid args.XMinorGrid args.YGrid args.YMinorGrid args.ZGrid args.ZMinorGrid args.YAxis args.Units args.Position args.InnerPosition args.OuterPosition args.PositionConstraint args.TightInset args.Colormap args.Alphamap args.ColorOrder args.ColorOrderIndex args.LineStyleOrder args.LineStyleOrderIndex args.LineStyleCyclingMethod args.FontUnits args.FontSize args.FontSizeMode args.TitleFontWeight args.SubtitleFontWeight args.TitleFontSizeMultiplier args.SortMethod args.Clipping args.NextPlot args.Toolbar args.Interactions args.InteractionOptions args.ToolbarLocation args.ToolbarLocationMode args.Children args.Parent args.Visible args.HandleVisibility args.Layout args.NextSeriesIndex args.ButtonDownFcn args.ContextMenu args.BusyAction args.BeingDeleted args.Interruptible args.CreateFcn args.DeleteFcn args.Type args.Tag args.UserData args.Selected args.SelectionHighlight args.HitTest args.PickableParts args.Legend

Sign in to comment.

Categories

Products

Release

R2023b

Asked:

on 19 May 2026 at 13:35

Edited:

about 6 hours ago

Community Treasure Hunt

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

Start Hunting!