how to find a non affected rank in a vector column

Dear matlab users,
in order to make lghter my script I treat the format of the legend of several plots in a dedicated zone of my script
figure(1); plot(x,y); h_leg(1) = legend('plot1')
...
figure(2); plot(x,y); h_leg(2) = legend('plot2')
...
figure(4); plot(x,y); h_leg(4) = legend('plot4')
...
%% overall treatment of the various legends
for j=1:4; set(h_leg(j),fontsize,12); end
here, the issue is that h_leg(3) does not exist and the last loop will fail in dealing with h_leg.
is there any mean to check that this class of object has some rank left undefined? A kind of isempty(h_leg(3)) for example
Thanks for your help
Cheers

 Risposta accettata

Probably for such a case as written
for j=1:4
try
set(h_leg(j),fontsize,12);
catch
end
end
will solve the problem; if you're out to optimize,
set(h_leg([1 2 4]),'fontsize',12)
altho the subscript vector should be coded dynamically instead of hardcoded (as should the loop be over 1:numel(h_leg) instead of 1:4)
What is the type of h_leg()?

2 Commenti

Many thanks for the feedback,
indeed the catch/try makes the job although a bit heavy.
and thanks for the advise on dynamical coding
h_leg is a "graphics" array with ranks 1 & 2 of "GraphicsPlaceholder" type and ranks 3 & 4 of "legend" type
Cheers
dpb
dpb il 2 Mag 2019
Modificato: dpb il 2 Mag 2019
I don't know that try...catch is any "heavier" than coding specifically to test whether every handle in the array of objects is a legend--the only way I can see to do that is to test strcmp(h_leg(i).Type,'legend') and it will also fail on the uninitialized object placeholder locations so would still have to either catch the error or do some other preliminary test.
I think it would be better to rearrange the code logic to either set the legend text property for those lines when they're being created or at least build the addressing array to them at that point instead.
AFAICT, there is no specific functionality provided to return a logical variable that a graphics placeholder array element is, as yet, unitiialized. That could, it seems, be a reasonable enhancement request.
I'll admit I've used gobject "in anger" precisely zero times to date so I'm far from adept in knowing what one can/cannot do with it; my penchant is to keep graphics handles in arrays of the specific type when the object(s) is/are created rather than in some catchall grab bag.

Accedi per commentare.

Più risposte (2)

You can use isvalid() or ishghandle()
set(h_leg(isvalid(h_leg(1:4))), 'fontsize', 12);
No loop needed. If h_leg is maximum size 4 then leave out the (1:4)

4 Commenti

Many thanks for the quick answer.
unfortunately the solution does not fit since h_leg is a 1x4 graphics arrray where:
ranks 1 and 2 are "GraphicsPlaceholder" and ranks 3 and 4 are of "legend" type.
This is something I want : to be able to comment some legends and plots among the whole list of plots an legends. Because of this, some ranks in the h_leg are not considered as a legend handle, and the "fontsize" property has no meaning.
That is why I would like to identify the non-legend type among the h_leg and hence avoid trying to set their non-existing "fontsize" property
Thanks again
Frédéric
In the case where you always want to deal with all of h_leg then
set(h_leg(ishghandle(h_leg)), 'fontsize', 12)
Frederic Cleva comments to me:
This makes the job too (ishghandle) !! Thanks
Indeed...either it or isgraphics turns out to return False for the uninitialized graphics object array elements -- I hadn't found either; the doc links are quite remiss at least on R2017b that is presently latest installed here (R2018b having mysteriously disappeared and not having had time to dig into why/reinstalling)...

Accedi per commentare.

Thanks for the helpfull clarification, all your remarks open some new tracks.
Regards

Categorie

Scopri di più su Creating, Deleting, and Querying Graphics Objects in Centro assistenza e File Exchange

Prodotti

Release

R2018a

Richiesto:

il 30 Apr 2019

Commentato:

dpb
il 2 Mag 2019

Community Treasure Hunt

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

Start Hunting!

Translated by