Is it possible to load a variable that can not be cleared

조회 수: 3 (최근 30일)
John
John 2019년 7월 12일
답변: Walter Roberson 2019년 7월 13일
I have a variable 'x' that is loaded using the startup file in ...\Documents\MATLAB\startup.m that executes the command
load x
I want this variable to remain in the workspace after executing
clear
in any of my MATLAB scripts or functions. Is this possible?
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 7월 12일
Which workspace does it need to stay in? Do any of the scripts need to execute at the command prompt, or are they all called within functions? Does the variable need to survive "clear all"?

댓글을 달려면 로그인하십시오.

답변 (2개)

John Doe
John Doe 2019년 7월 13일
clearvars -except x

Walter Roberson
Walter Roberson 2019년 7월 13일
Create a function named X.m that when given the argument 'load' loads the value of X from a file and puts it into a persistent variable and uses mlock
https://www.mathworks.com/help/matlab/ref/mlock.html
For any other argument, including no argument, index the persistent variable and return that result.
Limitations:
  1. does not permit the user to store into the variable
  2. does not permit use of end as an index
You can possibly improve on this by defining an object with subsref and subsasgn behavior.
You cannot simply mlock a variable however.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by