matlab 判断变量是否存在,在MATLAB中,是否有可能在创建一个对象之前检查一个对象是否已经存在?...

I'm trying to figure out how to ask the user whether they want to replace the previous object of the same class with the default object, or simply use the previous object, when calling the constructor.

I'm looking for actions in both these cases:

>>obj = Obj()

'obj' already exists. Replace it with default? (y/n): y

%clear obj and call default constructor to create new obj

>>obj = Obj()

'obj' already exists. Replace it with default? (y/n): n

%cancel call of Obj()

How would I do this? I've messed around with the default constructor, to no avail.

EDIT: If it makes any difference, Obj is a subclass of Handle.

解决方案

The following solution stems from several workarounds/hacks and is not part of the standard MATLAB's OO constructs. Use with caution.

You need to:

evalin() into the 'caller' workspace the names and classes of the 'base' workpsace variables

extract the name of the assigned variable with e.g. regexp()

compare names and classes. If a total match occurs, i.e. the variable in the 'base' workspace is being overwritten with a new instance of the same class, ask the user for input(). If the user chooses to preserve the existing object, overwrite the new instance with the existing one through evalin('caller',...).

The class foo:

classdef foo < handle

properties

check = true;

end

methods

function obj = foo()

% variable names and sizes from base workspace

ws = evalin('base','whos');

% Last executed command from window

fid = fopen([prefdir,'\history.m'],'rt');

while ~feof(fid)

lastline = fgetl(fid);

end

fclose(fid);

% Compare names and classes

outname = regexp(lastline,'\

if isempty(outname); outname = 'ans'; end

% Check if variables in the workspace have same name

idx = strcmp({ws.name}, outname);

% Ask questions

if any(idx) && strcmp(ws(idx).class, 'foo')

s = input(sprintf(['''%s'' already exists. '...

'Replace it with default? (y/n): '],outname),'s');

% Overwrite new instance with existing one to preserve it

if strcmpi(s,'n')

obj = evalin('caller',outname);

end

end

end

end

end

Class in action:

% create class and change a property from default (true) to false

clear b

b = foo

b =

foo with properties:

check: 1

b.check = false

b =

foo with properties:

check: 0

% Avoid overwriting

b = foo

'b' already exists. Replace it with default? (y/n): n

b

b =

foo with properties:

check: 0

The weaknesses (see points above):

applies only to cmw line and script executed commands, not functions (see link to extend to function calls). Also, might break in case of problems reading history.m.

the current regex fails on a==b.

Dangerous because the evalin() on user input leaves potential security threats open. Even if the input is filtered with the regexp and the string comparison, the construct might pose a problem if the code is revisited later on.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值