Clear Filters
Clear Filters

Keep the value of randperm after clear all

3 views (last 30 days)
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 Comments
Pawel Jastrzebski
Pawel Jastrzebski on 9 Feb 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 on 9 Feb 2018
Edited: Stephen23 on 9 Feb 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:

Sign in to comment.

Answers (1)

Jos (10584)
Jos (10584) on 9 Feb 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 Comment
Guillaume
Guillaume on 9 Feb 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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!