Keep the value of randperm after clear all

3 次查看(过去 30 天)
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 个评论
Pawel Jastrzebski
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 2018-2-9
编辑:Stephen23 2018-2-9
"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:

请先登录,再进行评论。

回答(1 个)

Jos (10584)
Jos (10584) 2018-2-9
  • 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 个评论
Guillaume
Guillaume 2018-2-9
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by