如何声明变量清除所有变量? [英] How to declare variables immune to clear all?

查看:105
本文介绍了如何声明变量清除所有变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MatLab中是否有声明变量可以清除的变量? 我想到的一种解决方案是保存变量,并在需要时重新打开它们. 谁能想到一个更优雅的解决方案?

Is there anyway to declare variables immune to clear all in MatLab? One solution I thought of was saving the variables and reopening them whenever I need them. Can anyone think of a more elegant solution?

让我更深入地解释我的问题,这本来应该是我应该做的;对此感到抱歉.

Let me explain my problem a bit more thouroughly, which I should have done in the first place; sorry for that.

我必须使用一些黑匣子"中间代码(其中一些可能是mex文件)运行一些例程.最好假设我不能深入研究这些代码.我可以更改其中的一些,但这会很昂贵.例如,我不知道一切在哪里发生.我知道我可能要求太多,但你永远都不会知道.

I have to run a few routines using some "black box" intermediate code (some of which may be mex files). It would be good to assume that I cannot dwell into these codes. I could alter some of them, but that would be costly; for example, I don't know where the clear all is happening. I know I may be asking for too much, but you never know.

推荐答案

您不能保护单个变量,但可以使用

You can't protect individual variables, but you can use mlock to prevent an M-file function or mex function from being cleared, and any persistent variables defined within.

clear all确实是一种便利. 它所做的不仅仅是清除变量.它不能代替理解代码的工作原理或使用函数来限制变量范围.如果您有不再使用的大型阵列,则可以明确地告诉Matlab清除它以节省内存.我敢打赌,您实际上想做的事情可以通过重新考虑代码的结构来解决.

clear all is really a convenience when one is using the Command Window directly or when writing quick scripts. It does a lot more than just clear variables. It's not a substitute for understanding how your code works or using functions to limit variable scope. If you have a large array that is no longer used, you can explicitly tell Matlab to clear it to save memory. I'd bet what you're actually trying to do could be solved by re-thinking the structure of your code.

这篇关于如何声明变量清除所有变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆