Check if object's property is empty

I have an xmlcomp.Edits object that was created when performing a comparison via the command
root = slxmlcomp.compare(...)
In this case, an xmlcomp.Edits object was created (see attached file) but all properties are empty. However, I don't know how to check if the properties are empty so I can catch the error that occurs when I try to access the properties. Is there a command to use when the object's properties are empty?

14 Kommentare

Rik
Rik am 20 Feb. 2022
Maybe numel works as expected?
>> numel(root)
ans =
1
I'm not sure how this helps in determining whether or not LeftFileName exists.
Image Analyst
Image Analyst am 20 Feb. 2022
Bearbeitet: Image Analyst am 23 Mär. 2022
What does this show
p = properties(xmlcomp)
if isfield(xmlcomp, 'Edits') && isempty(xmlcomp.Edits)
% Structure has the field but it's empty so do sommething with that knowledge.
end
Monika Jaskolka
Monika Jaskolka am 20 Feb. 2022
Ive J
Ive J am 20 Feb. 2022
Bearbeitet: Ive J am 20 Feb. 2022
r = load("root").root
r =
Edits with no properties.
isfield(r, "LeftFileName")
ans = logical
0
BTW, I think you still can catch the error when trying to access the property:
try; r.Version; catch; fprintf('oops! empty!\n'); end
oops! empty!
Monika Jaskolka
Monika Jaskolka am 20 Feb. 2022
Bearbeitet: Monika Jaskolka am 21 Feb. 2022
Even when not empty, isfield returns 0, so it doesn't look like it's the right function to use.
Also, I realize I can catch the error, but I want to have an if statement to prevent the error, like below. I just can't figure out what function I can use to check that the property exists.
try
if SOME_FUNCTION(root, 'LeftFileName')
% do something else because the data is missing
else
% proceed with getting the value
model_left = root.LeftFileName;
end
catch ME
% handle errors
end
per isakson
per isakson am 21 Feb. 2022
See: isprop - True if property exists
Monika Jaskolka
Monika Jaskolka am 21 Feb. 2022
Bearbeitet: Monika Jaskolka am 21 Feb. 2022
As the original question shows, isprop returns 1 when the property does not exist. Trying to access the property gives an error.
per isakson
per isakson am 21 Feb. 2022
Sorry for the noise.
Rik
Rik am 21 Feb. 2022
If you don't find anything, you could also put the if in catch block. That's what I do sometimes as well. (e.g. to catch a memory error, which isn't really possible to catch in advance)
Ive J
Ive J am 21 Feb. 2022
Bearbeitet: Ive J am 21 Feb. 2022
Easiest way that comes to my mind is struct itself:
r = load("root.mat").root;
r2 = load("root2.mat").root2;
warning off MATLAB:structOnObject
checkr = struct(r)
checkr = struct with fields:
BaseEdits: []
checkr2 = struct(r2)
checkr2 = struct with fields:
Filters: [1×3 struct] LeftFileName: 'C:\Users\bialym2\PhD\model1.slx' LeftRoot: [1×1 xmlcomp.Node] RightFileName: 'C:\Users\bialym2\PhD\model2.slx' RightRoot: [1×1 xmlcomp.Node] TimeSaved: '20-Feb-2022 18:26:07' Version: '2.0' BaseEdits: [1×1 xmlcomp.internal.edits.Edits]
warning off MATLAB:structOnObject
I'm confused. Could it be that 'TimeSaved' exists in root.mat and rootOP.mat, but isn't shown. I would ask @MathWorks Support Team. Anyhow, there is no property named 'missing'.
%% R2018b
OP = load("rootOP.mat");
r1 = load("root.mat");
r2 = load("root2.mat");
%%
isprop( r1.root, 'TimeSaved' )
ans = logical
1
isprop( r2.root2, 'TimeSaved' )
ans = logical
1
isprop( OP.root, 'TimeSaved' )
ans = logical
1
isprop( OP.root, 'missing' )
ans = logical
0
Henry Barth
Henry Barth am 23 Mär. 2022
Bearbeitet: Henry Barth am 23 Mär. 2022
if you want a function for your check, you could implement it for your needs like this:
function ispropOut = SOME_FUNCTION(objIn,nameIn)
ispropOut = false(size(objIn));
for nObjRow = 1:size(objIn,1)
for nObjCol = 1:size(objIn,2)
try
objIn(nObjRow,nObjCol).(nameIn);
ispropOut(nObjRow,nObjCol) = true;
catch
end
end
end
end
Adam Danz
Adam Danz am 23 Mär. 2022
I second Rik's advice.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Fangjun Jiang
Fangjun Jiang am 23 Mär. 2022

4 Stimmen

Might this help?
>> EmptyRoot=xmlcomp.Edits('')
EmptyRoot =
Edits with no properties.
>> isequal(root,EmptyRoot)
ans =
logical
1
>> isequal(root2,EmptyRoot)
ans =
logical
0

Kategorien

Mehr zu Data Type Identification finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2021b

Gefragt:

am 20 Feb. 2022

Beantwortet:

am 23 Mär. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by