Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Updating variables in declared function handles

Subject: Updating variables in declared function handles

From: kees de Kapper

Date: 13 Mar, 2013 05:13:11

Message: 1 of 8

Hi Folks,
I've got trouble with updating the input data (variables) of a function which is part of a struct.

In an matlab-file MyFunc.m I have defined the following two functions:
> function varargout = MyFunc(varargin)
> TmpStruct.Data = [];
> TmpStruct.Size = @(DataDimension)GetDataSize(DataDimension, TmpStruct.Data);
>
> function SizeOut = GetDataSize(DataDimension, Data)
> SizeOut = size(Data, DataDimension);

If I now do:
> TmpS = MyFunc();
> TmpS.Data = ones(20,30,40);
> TmpS.Size(1)
I would like to obtain the value '20', however I retrieve '0' since 'TmpStruct.Data' was defined empty before the function declaration. How could I "update" the input data/variable?

Thanks for help!

Kind regards,
Kees

Subject: Updating variables in declared function handles

From: kees de Kapper

Date: 13 Mar, 2013 10:45:07

Message: 2 of 8

After some tutorials and searches, I concluded that the Object Oriented classdef could help me. I'm going to follow that path. Thanks for your consideration.

Kees

"kees de Kapper" <kees_de_kapper@hotmail.com> wrote in message <khp1t7$3gv$1@newscl01ah.mathworks.com>...
> Hi Folks,
> I've got trouble with updating the input data (variables) of a function which is part of a struct.
>
> In an matlab-file MyFunc.m I have defined the following two functions:
> > function varargout = MyFunc(varargin)
> > TmpStruct.Data = [];
> > TmpStruct.Size = @(DataDimension)GetDataSize(DataDimension, TmpStruct.Data);
> >
> > function SizeOut = GetDataSize(DataDimension, Data)
> > SizeOut = size(Data, DataDimension);
>
> If I now do:
> > TmpS = MyFunc();
> > TmpS.Data = ones(20,30,40);
> > TmpS.Size(1)
> I would like to obtain the value '20', however I retrieve '0' since 'TmpStruct.Data' was defined empty before the function declaration. How could I "update" the input data/variable?
>
> Thanks for help!
>
> Kind regards,
> Kees

Subject: Updating variables in declared function handles

From: Steven_Lord

Date: 13 Mar, 2013 13:31:26

Message: 3 of 8



"kees de Kapper" <kees_de_kapper@hotmail.com> wrote in message
news:khp1t7$3gv$1@newscl01ah.mathworks.com...
> Hi Folks,
> I've got trouble with updating the input data (variables) of a function
> which is part of a struct.
>
> In an matlab-file MyFunc.m I have defined the following two functions:
>> function varargout = MyFunc(varargin)
>> TmpStruct.Data = [];
>> TmpStruct.Size = @(DataDimension)GetDataSize(DataDimension,
>> TmpStruct.Data);

This makes a COPY of TmpStruct.Data.

>> function SizeOut = GetDataSize(DataDimension, Data)
>> SizeOut = size(Data, DataDimension);
>
> If I now do:
>> TmpS = MyFunc();
>> TmpS.Data = ones(20,30,40);
>> TmpS.Size(1)
> I would like to obtain the value '20', however I retrieve '0' since
> 'TmpStruct.Data' was defined empty before the function declaration. How
> could I "update" the input data/variable?

As written, you'd need to recreate the anonymous function.

http://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html#f4-71621

"To supply different values for the coefficients, you must create a new
function handle:"

Since your next message said that you were switching your MyFunc function to
be an object instead, you could do this using dependent properties or a size
method on your object.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Updating variables in declared function handles

From: kees de Kapper

Date: 14 Mar, 2013 15:23:06

Message: 4 of 8

Thank you Steven!

I process brings up another question.

This is my Object defenition:

>> classdef MyTestObj
>> properties
>> Data = [];
>> end;
>>
>> methods
>> function obj=MyTestObj(M)
>> obj.Data = M;
>> end;
>> function obj = UpdateData(obj)
>> obj.Data = obj.Data+1;
>> end;
>> end;
>> end;

To create an Object instance I do:
>> obj=MyTestObj([1,2,3,4]);
Then, to add the value '1', I have to do:
>> obj = UpdateData(obj);
However, I'd like to use
>> UpdateData(obj);
and that the value of obj.Data is increased.

Is that possible? And how?

Many thanks in advance.

Kind regards,

Kees


"Steven_Lord" <slord@mathworks.com> wrote in message <khpv3e$o46$1@newscl01ah.mathworks.com>...
>
>
> "kees de Kapper" <kees_de_kapper@hotmail.com> wrote in message
> news:khp1t7$3gv$1@newscl01ah.mathworks.com...
> > Hi Folks,
> > I've got trouble with updating the input data (variables) of a function
> > which is part of a struct.
> >
> > In an matlab-file MyFunc.m I have defined the following two functions:
> >> function varargout = MyFunc(varargin)
> >> TmpStruct.Data = [];
> >> TmpStruct.Size = @(DataDimension)GetDataSize(DataDimension,
> >> TmpStruct.Data);
>
> This makes a COPY of TmpStruct.Data.
>
> >> function SizeOut = GetDataSize(DataDimension, Data)
> >> SizeOut = size(Data, DataDimension);
> >
> > If I now do:
> >> TmpS = MyFunc();
> >> TmpS.Data = ones(20,30,40);
> >> TmpS.Size(1)
> > I would like to obtain the value '20', however I retrieve '0' since
> > 'TmpStruct.Data' was defined empty before the function declaration. How
> > could I "update" the input data/variable?
>
> As written, you'd need to recreate the anonymous function.
>
> http://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html#f4-71621
>
> "To supply different values for the coefficients, you must create a new
> function handle:"
>
> Since your next message said that you were switching your MyFunc function to
> be an object instead, you could do this using dependent properties or a size
> method on your object.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

Subject: Updating variables in declared function handles

From: kees de Kapper

Date: 14 Mar, 2013 15:52:05

Message: 5 of 8

A small update.

I changed "classdef MyTestObj" into "classdef MyTestObj < handle"

Now the object itself will be updated. However, when i do "UpdateData(obj);", I will obtain "obj", and "ans" which is a copy of "obj". How can I get rid of "ans", without "clear ans"?

Many thanks in advance.

kind regards,
Kees.

Subject: Updating variables in declared function handles

From: Steven_Lord

Date: 15 Mar, 2013 04:38:07

Message: 6 of 8



"kees de Kapper" <kees_de_kapper@hotmail.com> wrote in message
news:khsrn5$4bu$1@newscl01ah.mathworks.com...
> A small update.
>
> I changed "classdef MyTestObj" into "classdef MyTestObj < handle"
>
> Now the object itself will be updated. However, when i do
> "UpdateData(obj);", I will obtain "obj", and "ans" which is a copy of
> "obj". How can I get rid of "ans", without "clear ans"?

Define UpdateData with 0 output arguments.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Updating variables in declared function handles

From: kees de Kapper

Date: 15 Mar, 2013 09:36:07

Message: 7 of 8

"Steven_Lord" <slord@mathworks.com> wrote in message <khu8jf$21d$1@newscl01ah.mathworks.com>...
"kees de Kapper" <kees_de_kapper@hotmail.com> wrote in message
news:khsrn5$4bu$1@newscl01ah.mathworks.com...

    Define UpdateData with 0 output arguments.

Indeed that works, but then obj isn't updated.

/Kees

Subject: Updating variables in declared function handles

From: Steven_Lord

Date: 15 Mar, 2013 13:24:32

Message: 8 of 8



"kees de Kapper" <kees_de_kapper@hotmail.com> wrote in message
news:khuq27$hs7$1@newscl01ah.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <khu8jf$21d$1@newscl01ah.mathworks.com>...
> "kees de Kapper" <kees_de_kapper@hotmail.com> wrote in message
> news:khsrn5$4bu$1@newscl01ah.mathworks.com...
>
> Define UpdateData with 0 output arguments.
>
> Indeed that works, but then obj isn't updated.

If your object is a value object, you will need to return the modified copy
of the object from within the method.
If your object is a handle object, you do not need to return the object from
the method, as modifying it within the method will modify the object
"referred to" by the variable you passed into the method.

For a more in-depth description of the difference between the two types,
read through this section in the documentation:

http://www.mathworks.com/help/matlab/matlab_oop/comparing-handle-and-value-classes.html

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us