How to count the number of times I called a function (using the command line)
59 次查看(过去 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!!
0 个评论
采纳的回答
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 个评论
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
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.
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Export to MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!