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

조회 수: 104 (최근 30일)
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!!

채택된 답변

per isakson
per isakson 2020년 6월 2일
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
William Pang
William Pang 2020년 6월 2일
편집: William Pang 2020년 6월 2일
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 2020년 6월 2일
"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.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Steven Lord
Steven Lord 2020년 6월 2일
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
William Pang
William Pang 2020년 6월 2일
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!!!

댓글을 달려면 로그인하십시오.


Ranitha Mataraarachchi
Ranitha Mataraarachchi 2020년 11월 16일
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.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by