Error with function handles of object methods
WMennerich
W.Mennerich at gmx.net
Wed Jun 24 09:20:57 CDT 2009
Hello,
I found a bug using objects:
In short words: Function handles of object functions (methods) within
the class directory (beginning with '@') can not be created.
I use Octave V3.2.0 (mingw binary) on WindowsXP.
I hope, this bug report contains enough details,
otherwise just ask please.
W.Mennerich
In the following the tests I made:
function test (<scalar>,<scalar>)
[constructor in the '@test' directory
*********************************
function newtest=test(att1, att2)
%constructor
switch nargin
case 0
newtest.att1=[];
newtest.att2=[];
case 2
newtest.att1=att1;
newtest.att2=att1+att2;
end
newtest=class(newtest,'test');
**********************************
function sumAtts(<test>)
[a method in the '@test' directory
**********************************
function res=sumAtts(this)
res=this.att1+this.att2;
function get(<test>,<string>)
[for testing function overloading,
also inside '@test']
**********************************
function att=get(this,nameAtt)
att=this.(nameAtt);
Test environment:
##################################
##################################
* Create all files
* Create '@test'
* Put all files into '@test'
* Put '@test' into octave path
Running the tests:
##################################
##################################
%Testing the class:
%create three objects and put them into a cell array:
mytest1=test(33,44)
mytest2=test(66,88)
mytest3=test(132,176)
allmytests={mytest1 mytest2 mytest3}
%Workspace of octave shows:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
allmytests 1x3 48 cell
mytest1 1x1 16 test
mytest2 1x1 16 test
mytest3 1x1 16 test
%Testing of the sumAtts(<test>) function:
> sumAtts(mytest1)
ans = 110
> sumAtts(mytest2)
ans = 220
> sumAtts(mytest3)
ans = 440
%Function 'sumAtts' as input of an anonymous function works:
> cellfun(@(x) sumAtts(x),allmytests)
ans =
110 220 440
%Also the (overloaded) function 'get' works fine:
> cellfun(@(x) get(x,'att1'),allmytests)
ans =
33 66 132
%Using cellfun with a sumAtts handle does not work:
> cellfun(@sumAtts,allmytests)
error: error creating function handle "@sumAtts"
error: evaluating argument list element number 1
error: error creating function handle "@sumAtts"
error: evaluating argument list element number 1
%The same also with overloaded 'get' function, gives another error:
> inStrings={'att1' 'att1' 'att1'}
inStrings =
{
[1,1] = att1
[1,2] = att1
[1,3] = att1
}
> cellfun(@get,allmytests,inStrings)
error: octave_base_value::array_value(): wrong type argument `class'
error: get: expecting graphics handle as first argument
%Try to create a function handle outside of cellfun:
> fun=@sumAtts
error: error creating function handle "@sumAtts"
%Non-class-functions work as handle:
> h=@rand
h =
rand
> h(4)
ans =
0.86496 0.62121 0.37157 0.41806
0.51272 0.49135 0.41006 0.67037
0.64824 0.25499 0.91680 0.27943
0.42406 0.77885 0.16088 0.15369
%Workspace of octave shows:
Attr Name Size Bytes Class
==== ==== ==== ===== =====
allmytests 1x3 48 cell
h 1x1 0 function_handle
inStrings 1x3 12 cell
mytest1 1x1 16 test
mytest2 1x1 16 test
mytest3 1x1 16 test
I checked also creating handles from selfwritten functions like e.g. foo.m
this works also fine. Just creating handles from functions which belong to
a user-class (i.e. they are here in the @test directory fail as handle)
--
View this message in context: http://www.nabble.com/Error-with-function-handles-of-object-methods-tp24185732p24185732.html
Sent from the Octave - Bugs mailing list archive at Nabble.com.
More information about the Bug-octave
mailing list