H5L.iterate exception and behavior of native matlab functions() method

1 view (last 30 days)
Hello,
I'm developing a wrapper to use hdf5 files via set of low-level [+H5(?)] packages, provided my Matlab. As a part of this project I need to use H5L.iterate method, that has the following signature:
[status idx_out opdata_out] = H5L.iterate(group_id,index_type,order,idx_in,iter_func,opdata_in)
where iter_func is a pointer to a callback function that is supposed to be used on every step of iteration.
The H5L.iterate method is called inside a general class (let's say hdf5Adapter), so I would like to use a handle to another method of hdf5Adapter as [iter_func] argument for H5L.iterate.
However when doing so I receive an exception thrown by H5L.iterate caused by the following validation inside H5L.iterate (%matlabroot\toolbox\matlab\imagesci\+H5L\iterate.m) :
f = functions(iter_func);
if isempty(f.file)
error(message('MATLAB:imagesci:H5:badIterateFunction'));
end
because f.file appears to be empty.
I have tested the behavior of functions() method when applied to class methods (example below) and it seems to return empty 'file' field in the return data structure. See the test class definition in the attachment.
The way I test it:
foo = tstClass();
disp('instance method, as object');
ifhi = @foo.testFuncInstance; % instance method, as object
disp(functions(ifhi));
disp('instance method, as class');
cfhi = @tstClass.testFuncInstance; % instance method, as class
disp(functions(cfhi));
disp('static method, as object');
ifhs = @foo.testFuncInstance; % static method, as object
disp(functions(ifhs));
disp('static method, as class');
cfhs = @tstClass.testFuncInstance; % static method, as class
disp(functions(cfhs));
My questions are the following:
  1. Is there any way to make the result struc of functions(handle_to_class_method) to have a non-trivial 'file' value?
  2. What is the purpose of the check 'if isempty(f.file)' in H5L.iterate?
Thank you, Maxim Semyonov
  1 Comment
Francesco Zucchi
Francesco Zucchi on 16 Sep 2015
I will just put the tstClass here as well
%%this is my test class
classdef tstClass % < handle % uncomment to test a handle class
% test class to be used to check native Matlab functions() behaviour
properties ( SetAccess = public, GetAccess = public )
state = '';
end
methods ( Access = public )
function obj = tstClass()
obj.state = 'new object';
end
end
methods ( Access = public )
function val = testFuncInstance(obj)
disp('in the testFuncInstance');
val = 0;
end
end
methods ( Static, Access = public )
function val = testFuncStatic()
disp('in the testFuncStatic');
val = 0;
end
end
end

Sign in to comment.

Accepted Answer

Philip Borghesani
Philip Borghesani on 16 Sep 2015
Edited: Philip Borghesani on 16 Sep 2015
I do not know why H5L.iterate needs function handles with a file defined. I suggest you file an enhancement request to remove the requirement.
I can give you a workaround. You will need to make two changes.
  1. Create your function handles from a function not a script.
  2. The instance method as class and static method as class function handles must be created in the form of an anonymous function:
cfhi = @() tstClass.testFuncInstance; % instance method, as class
cfhs = @() tstClass.testFuncStatic; % static method, as class
If you look at the function line the parser is doing this for you but because it is generating the code the file is not being filled in. In the case of a script the owning file is the calling function or the base workspace.
Just a note here you can't call an instance method in the form classname.method not sure why you are testing that. My recomended syntax for a call to an instance method for iterating would be:
fh=@(loc_id,attr_name) myIterateMethod(obj, loc_id, attr_name);
  4 Comments
Philip Borghesani
Philip Borghesani on 17 Sep 2015
Edited: per isakson on 15 May 2016
The only way to make both checks pass is to create a local or nested helper function with the correct signature. Anonymous functions can't be made to return a defined number of outputs. If a static method is used than a local function will work otherwise it will need to be nested.
Incomplete psudocode for nested function use:
function demo
obj=makeobj;
fh=@nestedHelper
% use fh here
%...
function [status, opdata_out] = nestedHelper(group_id,name,opdata_in)
[status, opdata_out] =obj.method(group_id,name,opdata_in) ;
end
end
I believe that iterate is being much to restrictive about the types of function handle it will accept.
Francesco Zucchi
Francesco Zucchi on 17 Sep 2015
That definitely works, though non-intuitive for me and probably requires the knowledge of internal parser logic of Matlab...
Thank you very much Philip, your guidance is highly appreciated

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!