Borrar filtros
Borrar filtros

图形对象的父级在parfor中无法读取

6 visualizaciones (últimos 30 días)
闻龙
闻龙 el 14 de Mzo. de 2023
Respondida: Himanshu el 30 de Mzo. de 2023
在parfor中,我无法通过 h.Parent() 的方式读取到图形对象的父对象,使用 ancestor(h, "Figure") 方法同样不行,这是为什么?
H = gobjects(3, 1);
figure("Name", "fig1", Visible="off"), H(1) = axes(); plot(1:10);
figure("Name", "fig2", Visible="off"), H(2) = axes(); plot(1:10);
figure("Name", "fig3", Visible="off"), H(3) = axes(); plot(1:10);
% for-loop
for i = 1:3
disp(class(H(i))); % matlab.graphics.axis.Axes
disp(class(H(i).Parent)); % matlab.ui.Figure
disp(class(ancestor(H(i), "figure"))); % matlab.ui.Figure
disp(H(i).Parent.Name); % Fig i
end
matlab.graphics.axis.Axes
matlab.ui.Figure
matlab.ui.Figure
fig1
matlab.graphics.axis.Axes
matlab.ui.Figure
matlab.ui.Figure
fig2
matlab.graphics.axis.Axes
matlab.ui.Figure
matlab.ui.Figure
fig3
% parfor-loop
parfor i = 1:3
disp(class(H(i))); % matlab.graphics.axis.Axes
disp(class(H(i).Parent)); % matlab.graphics.GraphicsPlaceholder, why?
disp(class(ancestor(H(i), "figure"))); % double, why?
disp(H(i).Parent.Name); % error
end
Starting parallel pool (parpool) using the 'Processes' profile ... Connected to the parallel pool (number of workers: 2). matlab.graphics.axis.Axes matlab.graphics.GraphicsPlaceholder double matlab.graphics.axis.Axes matlab.graphics.GraphicsPlaceholder double
Unrecognized method, property, or field 'Name' for class 'matlab.graphics.GraphicsPlaceholder'.

Respuesta aceptada

Himanshu
Himanshu el 30 de Mzo. de 2023
Hello,
As per my understanding, you are facing an error while using "parfor" loop and reading the parent of a graphical object. The error occurs because accessing the properties of the parent figure or working with graphical objects directly is not supported within "parfor" loops.
When using "parfor" in MATLAB, graphical objects behave differently than in a regular "for" loop due to the nature of parallel computing. In a "parfor" loop, MATLAB creates separate worker instances that execute the loop iterations independently and in parallel. Each worker has its own workspace and does not have access to the GUI.
You can refer to the below documentation to understand more about "parfor" loops.

Más respuestas (0)

Categorías

Más información sobre Parallel for-Loops (parfor) en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!