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:
Counting the number of times function is used

Subject: Counting the number of times function is used

From: Francki

Date: 19 Aug, 2010 19:04:28

Message: 1 of 9

hi guys,

assuming I have a function how can count the number of times the function was used? (i.e. the number of operation performed using the function).

Example:

function [v,d]=adddivide(x1,x2)
v=x1+x2;
d=x1/x2;
end

now say I have performed few operations using this function......how can I count the number of time adddivide was used?

I have sort of some idea but any1 guide me:

function [v,d]=adddivide(x1,x2)
s.cnt=0;
if (x2~=0)
v=x1+x2;
d=x1/x2;
s.cnt=s.cnt+1;
end
end

not sure why but "s.cnt" keeps returning 1 everytime I run the function....help would be appreciated.

Thanks

Subject: Counting the number of times function is used

From: Matt Fig

Date: 19 Aug, 2010 19:15:37

Message: 2 of 9

Among the ways to approach this: See the help for PERSISTENT.

Subject: Counting the number of times function is used

From: Sean

Date: 19 Aug, 2010 19:21:10

Message: 3 of 9

"Francki " <jonesfranckandi@yahoo.fr> wrote in message <i4jv7s$p61$1@fred.mathworks.com>...
> hi guys,
>
> assuming I have a function how can count the number of times the function was used? (i.e. the number of operation performed using the function).
>
> Example:
>
> function [v,d]=adddivide(x1,x2)
> v=x1+x2;
> d=x1/x2;
> end
>
> now say I have performed few operations using this function......how can I count the number of time adddivide was used?
>
> I have sort of some idea but any1 guide me:
>
> function [v,d]=adddivide(x1,x2)
> s.cnt=0;
> if (x2~=0)
> v=x1+x2;
> d=x1/x2;
> s.cnt=s.cnt+1;
> end
> end
>
> not sure why but "s.cnt" keeps returning 1 everytime I run the function....help would be appreciated.


s.cnt is reset to 0 every time the function is called. If you want to do something like this global, persistent, and appdata should be used:

function [out] = foo(in)
   global count
   count = count+1
%other stuff
end

This would be easily retrievable from the command window as well by defining global count there. However, you would have to remember to clear global count every time you wanted to do something. You could also use persistent:

function [out count] = foo(in)
   persistent count
   count = count+1
%other stuff
end
Once again clean up would be important.

And of course last and definitely not least

preallocate count
setappdata(0,'count',0)

calls to foo...
function [out count] = foo(in)
   count = getappdata(0,'count);
   count = count+1
   setappdata(0,'count',count);
%other stuff
end

This wins because it can be passed back as an argument AND retrieved from the command window directly
getappdata(0,'count)

It would also need to be cleared or reset

Subject: Counting the number of times function is used

From: Francki

Date: 19 Aug, 2010 19:24:06

Message: 4 of 9

"Matt Fig" <spamanon@yahoo.com> wrote in message <i4jvsp$90m$1@fred.mathworks.com>...
> Among the ways to approach this: See the help for PERSISTENT.
sample code please. persistent doesn't explain much. how do you apply it.
thanks

Subject: Counting the number of times function is used

From: Francki

Date: 19 Aug, 2010 20:54:23

Message: 5 of 9

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i4k076$tj$1@fred.mathworks.com>...
> "Francki " <jonesfranckandi@yahoo.fr> wrote in message <i4jv7s$p61$1@fred.mathworks.com>...
> > hi guys,
> >
> > assuming I have a function how can count the number of times the function was used? (i.e. the number of operation performed using the function).
> >
> > Example:
> >
> > function [v,d]=adddivide(x1,x2)
> > v=x1+x2;
> > d=x1/x2;
> > end
> >
> > now say I have performed few operations using this function......how can I count the number of time adddivide was used?
> >
> > I have sort of some idea but any1 guide me:
> >
> > function [v,d]=adddivide(x1,x2)
> > s.cnt=0;
> > if (x2~=0)
> > v=x1+x2;
> > d=x1/x2;
> > s.cnt=s.cnt+1;
> > end
> > end
> >
> > not sure why but "s.cnt" keeps returning 1 everytime I run the function....help would be appreciated.
>
>
> s.cnt is reset to 0 every time the function is called. If you want to do something like this global, persistent, and appdata should be used:
>
> function [out] = foo(in)
> global count
> count = count+1
> %other stuff
> end
>
> This would be easily retrievable from the command window as well by defining global count there. However, you would have to remember to clear global count every time you wanted to do something. You could also use persistent:
>
> function [out count] = foo(in)
> persistent count
> count = count+1
> %other stuff
> end
> Once again clean up would be important.
>
> And of course last and definitely not least
>
> preallocate count
> setappdata(0,'count',0)
>
> calls to foo...
> function [out count] = foo(in)
> count = getappdata(0,'count);
> count = count+1
> setappdata(0,'count',count);
> %other stuff
> end
>
> This wins because it can be passed back as an argument AND retrieved from the command window directly
> getappdata(0,'count)
>
> It would also need to be cleared or reset

hi there mate
the get and setappdata work
but I have to enter at the command window "setappdata(0,'count',0)"; how can I avoid that? is there a way to enter it from the m-file and compute it so that it sets the application to zero and everytime you run the code it adds 1?......
thanks

Subject: Counting the number of times function is used

From: Steven_Lord

Date: 19 Aug, 2010 21:05:37

Message: 6 of 9



"Francki " <jonesfranckandi@yahoo.fr> wrote in message
news:i4jv7s$p61$1@fred.mathworks.com...
> hi guys,
>
> assuming I have a function how can count the number of times the function
> was used? (i.e. the number of operation performed using the function).

That depends on when you want to know this information.

If you're running your code and you want to know, at the end, how many times
each function was called use the PROFILE function to run your code in the
Profiler.

If you want to keep a running counter that increments each time the function
is called and you need to be able to access that counter during execution,
the PERSISTENT approach others in the thread have advocated is one solution.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Counting the number of times function is used

From: Francki

Date: 19 Aug, 2010 22:43:05

Message: 7 of 9

"Steven_Lord" <slord@mathworks.com> wrote in message <i4k6b1$d03$1@fred.mathworks.com>...
>
>
> "Francki " <jonesfranckandi@yahoo.fr> wrote in message
> news:i4jv7s$p61$1@fred.mathworks.com...
> > hi guys,
> >
> > assuming I have a function how can count the number of times the function
> > was used? (i.e. the number of operation performed using the function).
>
> That depends on when you want to know this information.
>
> If you're running your code and you want to know, at the end, how many times
> each function was called use the PROFILE function to run your code in the
> Profiler.
>
> If you want to keep a running counter that increments each time the function
> is called and you need to be able to access that counter during execution,
> the PERSISTENT approach others in the thread have advocated is one solution.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com

But how do you use persistent effectively
i've tried the following:

function [v d]=adddivide(x1,x2)
persistent count
count = count+1
if (x2~=0)
v=x1+x2;
d=x1/x2;
end
end

but all I get is an empty matrix (becasue count hasn't been defined)....So how do I set count to 0 so that the count adds 1 to its previous value?.....I don't want to type from the command window count=0 before doing some operations.....I want everything to be in the m-file......any help is welcome.
Thanks guys for the replies

Subject: Counting the number of times function is used

From: Matt Fig

Date: 19 Aug, 2010 22:53:07

Message: 8 of 9

Here is one way to do it with a PERSISTENT variable:


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [v,d] = adddivide(x1,x2)

persistent CNT
 
if isempty(CNT)
   CNT = 0;
else
    CNT = CNT + 1;
end

if ~nargin
   v = CNT;
   d = CNT;
   CNT = [];
   return
end

v = x1+x2;
d = x1/x2;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Now from the command line:
>> % First call function 20 times:
>> for ii = 1:20,[V,D] = adddivide(ii,ii);end
>> % Now check if the count is accurate:
>> adddivide % Calling with no args gives the call count but doesn't increment it.
ans =
    20
>> % Now check if we have a fresh start:
>> adddivide
ans =
     0

Subject: Counting the number of times function is used

From: Francki

Date: 19 Aug, 2010 23:10:10

Message: 9 of 9

"Matt Fig" <spamanon@yahoo.com> wrote in message <i4kckj$p6f$1@fred.mathworks.com>...
> Here is one way to do it with a PERSISTENT variable:
>
>
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
> function [v,d] = adddivide(x1,x2)
>
> persistent CNT
>
> if isempty(CNT)
> CNT = 0;
> else
> CNT = CNT + 1;
> end
>
> if ~nargin
> v = CNT;
> d = CNT;
> CNT = [];
> return
> end
>
> v = x1+x2;
> d = x1/x2;
> %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
>
> Now from the command line:
> >> % First call function 20 times:
> >> for ii = 1:20,[V,D] = adddivide(ii,ii);end
> >> % Now check if the count is accurate:
> >> adddivide % Calling with no args gives the call count but doesn't increment it.
> ans =
> 20
> >> % Now check if we have a fresh start:
> >> adddivide
> ans =
> 0

Excellent, you've made a very frustrating day into a more enjoyable one.
Thanks!!

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