필터 지우기
필터 지우기

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

조회 수: 6 (최근 30일)
闻龙
闻龙 2023년 3월 14일
답변: Himanshu 2023년 3월 30일
在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'.

채택된 답변

Himanshu
Himanshu 2023년 3월 30일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!