deleting a certain variable in the base workspace via a function

56 ビュー (過去 30 日間)
AA
AA 2015 年 1 月 23 日
編集済み: Will Reeves 2025 年 1 月 13 日
hi, assume i have the variable x in the base workspace.
I want to delete that variable x via a function. How can I accomplish this?
thanks
  3 件のコメント
Vassilis Lemonidis
Vassilis Lemonidis 2022 年 2 月 24 日
Design flaws and scripts (workspaces) do not go along in general, so personally I do not consider the above a valid argument. Also, let me reverse the argument. Eg. let's say you have a function saveInChunks that saves a huge variable, created by let's say a matrices product or any other convolved operation, in chunks, to pass it to parfor workers, and then deletes the variable. This cannot be implemented anywhere outside of the main script that creates the variable in the first place. That is a design flaw, of Matlab. Unfortunately, to my knowledge, the only way to handle such a situation is by using evalin , which is an intrinsically unsafe operation, or well, make classes and have to face all the backwards compatibility issues and caveats, without live debugging.
Steven Lord
Steven Lord 2022 年 2 月 24 日
If you've written one function that interacts with a different workspace than its own, what guarantee do you have that you don't have two functions that are both trying to interact with that different workspace? If the first of those functions deletes or modifies data that the second of those functions needs to use, that first function could break the second function (and if it just modifies the data rather than deleting it, that "break" could be a change in the behavior of that second function that gives no indication the behavior has changed rather than an error. Those can be very difficult and time consuming to debug.)

サインインしてコメントする。

採用された回答

per isakson
per isakson 2015 年 1 月 23 日
編集済み: per isakson 2015 年 1 月 23 日
Try
evalin( 'base', 'clear(''x'')' )
and simpler
evalin( 'base', 'clear x' )
But take the advice of Image Analyst seriously
  1 件のコメント
Will Reeves
Will Reeves 2025 年 1 月 13 日
編集済み: Will Reeves 2025 年 1 月 13 日
being pedantic, "deleting" and "clearing" are different things in Matlab... Be careful which one is choosen!
Specifically, when "deleteing" instances of classes that have a "destructor" method, "clear" will not trigger it and could leave you with issues. In that case "delete" must be called first (which triggers the delete method), then "clear" which cleans up the handle to the class instance.

サインインしてコメントする。

その他の回答 (2 件)

Image Analyst
Image Analyst 2015 年 1 月 23 日
That would not be necessary. It's not good practice to have functions messing around with variables in other workspaces than their own.
  1 件のコメント
Sean de Wolski
Sean de Wolski 2015 年 1 月 23 日
If you need to save something to the base workspace and thus want to clear what's there, look at export2wsdlg. This helps the user not mess anything up.

サインインしてコメントする。


dpb
dpb 2015 年 1 月 23 日

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by