MATLAB: Saving existing matrix

existif statementlogical?

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

Best Answer

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?