Saving existing matrix

3 views (last 30 days)
Nicolas
Nicolas on 19 Apr 2011
Hi,
I would like to find a way to tell my code that if at the end of the loop the matrix 'P' exists then to save it, if not to continue the loop. I wrote that:
z=exist('P','var')
if z>0
save
end
It is wroking but, is it a good way to write it?
Cheers

Accepted Answer

Matt Fig
Matt Fig on 19 Apr 2011
Why would P exist sometimes and not others? If it is created somewhere in the loop, presumably an IF statement or other such branch, why not just save it there? Since your saved variable will be overwritten each time through the loop (that P exists), why not save after the loop?
  1 Comment
Nicolas
Nicolas on 19 Apr 2011
basically, I'm studying a lot of data with the same range of parameters (i have 3 nested loops). For some data and some parameters P doesn't exist, while for the same data and different parameters P exists.
I'm saving P outside the loop (as you mentionned) and each time it is saved like P-1-1, then P-1-2 and so on.
basically I'm trying to make my matlab writting a bit better, and i try to avoid "if" or "for" when not necessary, to make my code more efficient.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!