フィルターのクリア

图形对象的父级在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 Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!