How to count the number of times I called a function (using the command line)

99 views (last 30 days)
Hi,
The backstory: So I'm trying to design an image analysis tool on App Designer where I "feed" an image through a function, do some analysis in my function, output numerical results, verfiy to see if the analysis was done correctly visually, save the function outputs into a spreadsheet, and repeat the process with a different image. The first time the function is called, the results will be stored in the first row of the array; the second time it is called it will be stored in the second row of the array so on and so forth. I figured that to do this, I need to have someway to keep track of how many times my function is called. So I'm testing this with a simpler example:
function p = myfunction(a,b,c)
counter=0; %Initializing counter
parabola
function parabola
e = a+5
f = b+10
g= c+100
store=[e,f,g] %Still figuring out how to define the row associated with the counter to store my variable
end
counter = counter+1
end
However, I'm at to how I can not make the counter reset everytime I pass a new function.
For instance, if I were to do myfunction(1,2,3) followed by myfunction(2,3,4) I would still get counter =1.
Any help for this novice will be highly appreciated!!

Accepted Answer

per isakson
per isakson on 2 Jun 2020
You could replace
counter=0; %Initializing counter
by
persistent counter
if isempty( counter )
counter=0; %Initializing counter
end
However, don't you have the same problem with store ?
  2 Comments
William Pang
William Pang on 2 Jun 2020
Edited: William Pang on 2 Jun 2020
Thanks for your answer. That's a good point as well; I tried and I'm seeing similar problems (i.e. the first row of my store matrix gets set to 0)
function p = myfunction(a,b,c)
persistent counter
if isempty( counter )
counter=0; %Initializing counter
end
store=zeros(10,3) %I guess the number of rows doesn't really matter
parabola
function parabola
e = a+5
f = b+10
g= c+100
store(counter+1,:)=[e,f,g]
end
counter = counter+1
end
Maybe there is a more elegant solution that I'm not seeing?
per isakson
per isakson on 2 Jun 2020
"save the function outputs into a spreadsheet" does that mean that you eventually will want to transfer the content of store to the spreadsheet?
Now the current value of store will be lost every time myfunction is finished. The output p isn't set.
Why not write directly to the spreadsheet? A bit slow but ...
The more elegant solution is based on a class with method to add rows, write to spreadsheet, and it will take care of the counter.

Sign in to comment.

More Answers (2)

Steven Lord
Steven Lord on 2 Jun 2020
Since you're doing this as part of an App Designer app, why not store the iteration count in a property of your app? As long as you have a handle to the app in one of the functions being called, it can either retrieve and update that property directly or pass the property value into a function that's not an app method, receive an updated value from that function when it returns, and store the updated value back into the property.
  1 Comment
William Pang
William Pang on 2 Jun 2020
Thanks Steven for the suggestion! I tried it and it works. However, I'm still facing one problem:
%Passes through RangeFilter Function
[s, per, I_bin_regions_show] = rangefiltertest(app.I_M,filter_size, Thresh, cut_cells_regions); %My function
app.ValueEditField.Value = num2str(s); %Displays sum into box — to be placed in excel
app.PercentCoverageEditField.Value = num2str(per); %Displays percentage into box — to be placed in excel
%Store Image
store(app.TotalCount,:) = [s, per]
writematrix(store,'RangeFilterData.xls')
where I have the app.TotalCount linked to a button in my App Designer:
%Clears boxes when "Next" is pressed
app.UIAxes.cla;
app.UIAxes_2.cla;
app.ValueEditField.Value = '';
app.PercentCoverageEditField.Value ='';
%Increment Display
app.TotalCount = app.increment();
app.Label.Text=num2str(app.TotalCount);
Here's a snapshot of my UI:
The idea is whenever you load an image, it will run through my function and give out two numbers (values and percent coverage) that I want stored in my Excel sheet. Once you click "next", you clear the screen, and then you can load a new image and repeat the same analysis. Once you've completed, all the data would ideally be saved into an excel sheet.
However, when I try to upload the second image:
store(app.TotalCount,:) = [s, per]
writematrix(store,'RangeFilterData.xls')
I see the data is all messed up on my excel sheet:
Any help would be appreciated!!!

Sign in to comment.


Ranitha Mataraarachchi
Ranitha Mataraarachchi on 16 Nov 2020
Hi. Maybe you found yourself a way out. But you could've defined the 'counter' as a global variable and increment the counter inside the function.

Community Treasure Hunt

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

Start Hunting!