Borrar filtros
Borrar filtros

Saving existing matrix

1 visualización (últimos 30 días)
Nicolas
Nicolas el 19 de Abr. de 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

Respuesta aceptada

Matt Fig
Matt Fig el 19 de Abr. de 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 comentario
Nicolas
Nicolas el 19 de Abr. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Logical en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by