saving variables contained within a function
Show older comments
I need to save variables that are computed inside a function (called from a header) without providing them as output parameters. I can't use 'save' because the function is called inside a loop and it overwrites (the variables computed change over time). Is the only way to accomplish this through 'fprintf'?
Accepted Answer
More Answers (2)
Rick Rosson
on 28 Jun 2011
Another approach would be to declare a persistent variable that will accumulate the results each time you call the function, and then perhaps write the results out to a MAT file the last time you call the function.
Please check the documentation:
doc persistent
The function template would be:
function y = foo(x)
persistent results
if isempty(results)
results = zeros(...);
end
...
...
end
HTH.
Categories
Find more on Cell Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!