set() function works on 2017a but crash on 2018b

2 次查看(过去 30 天)
The following codes works on 2017a, but failed on 2018b I use debugger step by step, and found out it crashes at set(axs,'Parent',p); on version 2018b.
On version 2018b at line set(axs,'Parent',p); it will call the program usejava.m, but not 2017a.
p=uipanel(gcf,'Units','normalized','Position',...
[0 0.2 10.8],'BorderType','none','Tag','CursorPanel1');
set(p,'BackgroundColor',get(gcf,'Color'));
axs=findall(gcf,'Type','Axes');
set(axs,'Parent',p); <== crashes on this line on version 2018b, but it works on version2017a
Please help.
Thank you.
I have Matlab license.
  6 个评论
Jenny Khuon
Jenny Khuon 2020-6-10
I add try/catch ME, and it works. It display chart and table as I expected.
But I cannot submit the code to my customer, with no (blank) statement in catch ME block. It will be a big joke.
try
p=uipanel(gcf,'Units','normalized','Position',...
[0 0.2 1 0.8],'BorderType','none','Tag','CursorPanel1');
set(p,'BackgroundColor',get(gcf,'Color'));
axs=findall(gcf,'Type','Axes');
set(axs,'Parent',p);
catch ME
*** blank, no statement here. ***
end
Jenny Khuon
Jenny Khuon 2020-6-10
I need to use gcf. I did try without gcf, gca, gcbo, it could not run at all. Please see the message I just sent.
axs is:
3×1 graphics array:
Axes (Endurance Test I)
Axes
AxesToolbar
Although I can add a try/catch ME block, and I can get what I expected. But, I need to shutdonw the matlab, and re-launch again. Otherwise, it will crash the second time I run it again.

请先登录,再进行评论。

回答(1 个)

Adam Danz
Adam Danz 2020-6-10
编辑:Adam Danz 2020-6-10
First, some feedback,
The try-catch may have avoided an error message but that doesn't mean the problem was solved. The problematic line very likely still does not work and will just invoke the 'catch' code.
You don't need to use gcf. You need to use the actual figure handle or you can search for the figure handle. The (big) problem with gcf that Stephen Cobeldick pointed out is that you have no control over which figure it actually references. If more than 1 figure is available, even if it's a hidden figure, the user might click on a different figure and suddenly the rest of the code will be pointless. If the original figure is somehow deleted, gcf will create a new figure but in that case, you would want an error indicating an invalid figure handle.
Cause of the problem & the solution
"axis is a 3x1 graphics array".
Note that the 3rd element is not an axes, it's an AxisToolbar.
When you call set(axs,'Parent',p), it tries to see the parent of all 3 objects as p. As the error message indicates, "AxesToolbar cannot be a child of Panel.". To set the parent of the 2 axes,
set(axs(1:2),'Parent',p)
It's unclear why the axistoolbar is being identified in the call to findall(gcf,'Type','Axes');
  11 个评论
Jenny Khuon
Jenny Khuon 2020-6-15
For 2017a:
ax=findall(f,'Type','Axes');
For 2018b:
ax=findall(f,'Type','axes');
I have tested on 2017a, either Axes or axes is working.
But, for 2018a, it only accepts axes.
Hope this help.
Adam Danz
Adam Danz 2020-6-15
That's not correct. Here's the results for both upper and lower case "axes" in r2018a..
% Lower case
>> ax=findall(gcf,'Type','axes')
ax =
Axes with properties:
XLim: [0 1]
YLim: [0 1]
XScale: 'linear'
YScale: 'linear'
GridLineStyle: '-'
Position: [0.13 0.11 0.775 0.815]
Units: 'normalized'
Show all properties
% Upper case
>> ax=findall(gcf,'Type','Axes')
ax =
Axes with properties:
XLim: [0 1]
YLim: [0 1]
XScale: 'linear'
YScale: 'linear'
GridLineStyle: '-'
Position: [0.13 0.11 0.775 0.815]
Units: 'normalized'
Show all properties
% Show version number
>> ver
-----------------------------------------------------------------------------------------------------
MATLAB Version: 9.4.0.813654 (R2018a) % <-----
MATLAB License Number: xxxxxx
Operating System: Microsoft Windows 7 Professional Version 6.1 (Build 7601: Service Pack 1)
Java Version: Java 1.8.0_144-b01 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
-----------------------------------------------------------------------------------------------------

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by