How to count the number of times I called a function (using the command line)
59 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
William Pang
am 2 Jun. 2020
Beantwortet: Ranitha Mataraarachchi
am 16 Nov. 2020
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 Kommentare
Akzeptierte Antwort
per isakson
am 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 Kommentare
per isakson
am 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.
Weitere Antworten (2)
Steven Lord
am 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 Kommentar
Ranitha Mataraarachchi
am 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Data Export to MATLAB finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!