How can I delete variables whose name contains a specific word?

20 次查看(过去 30 天)
Let's say that I have three variables: var_red, var_blue, var_green, var2_blue
How can I specify to delete all the variables containing the word "blue"?
Thank you very much

采纳的回答

Massimo Zanetti
Massimo Zanetti 2016-10-9
The Matlab "clear" command accepts wildcards, so you can do
clear *blue;

更多回答(2 个)

Guillaume
Guillaume 2016-10-9
编辑:Guillaume 2016-10-9
See the clear documentation. The option that interests you is the -regexp one. In your case:
clear -regexp blue
will remove any variable with blue anywhere in their name.

Jan
Jan 2016-10-17
编辑:Jan 2016-10-17
Although the clear * methods works, consider that this is a bad programming pattern: Do not hide information in the names of variables. Data should be carried as value, not inside the name.
var(1).value = 123;
var(1).color = 'red';
var(2).value = 456;
var(2).color = 'blue';
toDelete = strcmp({'var.color'}, 'red');
var(toDelete) = [];
Then variables like "nabluencode" or "credentials" are not removed accidently.

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by