Effacer les filtres
Effacer les filtres

Keep the value of randperm after clear all

3 vues (au cours des 30 derniers jours)
Davidra Fantarina ANDRIAMISAINA
Modifié(e) : Stephen23 le 9 Fév 2018
I have this code in one script
order = randperm(rows*columns)
And one other script starts with clear all so how can i keep the value of order?
  4 commentaires
Pawel Jastrzebski
Pawel Jastrzebski le 9 Fév 2018
Sure thing. I'm still a learner but also believe that when developing a code (especially if you're new to Matlab), first:
  • 'get the code to work'
  • 'get the code to work better'
In other words, efficiency and elegance will come with the experience. Sometimes having a code that simply works (programmed awkwardly, but still works) is better than having no code at all.
Stephen23
Stephen23 le 9 Fév 2018
Modifié(e) : Stephen23 le 9 Fév 2018
"And one other script starts with clear all so how can i keep the value of order?"
So don't use clear all. clear all only slows down your code and is very very rarely required anyway, as clear's help states:
"Calling clear all, clear classes, and clear functions decreases code performance, and is usually unnecessary."
Write functions instead of scripts, they avoid all of these trivial problems by having independent workspaces:

Connectez-vous pour commenter.

Réponses (1)

Jos (10584)
Jos (10584) le 9 Fév 2018
  • learn to use functions rather than scripts, pass the variables needed for the second function as arguments. No need for clear all
  • use clearvars rather than clear all
  1 commentaire
Guillaume
Guillaume le 9 Fév 2018
Or rather don't use any sort of clear at all.
Any code that I'm given that contains clear all and/or close all, I instantly discard. Don't mess up my workspace, don't close my carefully constructed figures.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by