Saving existing matrix

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

 Risposta accettata

Matt Fig
Matt Fig il 19 Apr 2011

0 voti

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 Commento

Nicolas
Nicolas il 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by