气垫怎么用才能不浮粉的,如何才能远程定位

matlab常用测试函数_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
matlab常用测试函数
|0|0|文档简介
硕士研究生|
总评分4.3|
浏览量1129960
&&matlab常用测试函数
你可能喜欢测试测量论坛 -
最好最受欢迎电子论坛!
后使用快捷导航没有帐号?
分区版主: ,
主题: 2万, 帖数: <span title="万
主题: 3010, 帖数: 6万
主题: 2063, 帖数: 2万
主题: 2178, 帖数: 1万
Powered byI'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
retrieve the
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
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);
fclose(fid);
% Compare names and classes
outname = regexp(lastline,'\&[a-zA-Z]\w*(?=.*?=)','match','once');
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);
Class in action:
% create class and change a property from default (true) to false
foo with properties:
b.check = false
foo with properties:
% Avoid overwriting
'b' already exists. Replace it with default? (y/n): n
foo with properties:
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
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.
本文地址: &
我想知道如何询问用户是否要使用默认对象替换同一类的上一个对象,或者在调用构造函数时直接使用上一个对象。
我在这两种情况下都在寻找动作:
&& obj = Obj()'obj'已经存在。将其替换为默认值? (y / n):y %clear obj并调用默认构造函数来创建新的obj
&& obj = Obj()'obj'将其替换为默认值? (y / n):n %取消调用Obj()
做这个?我已经搞砸了默认构造函数,没有效果。
编辑:如果它有什么区别,Obj是Handle的子类。
解决方案 以下解决方案源于几种解决方法/ hacks,不是标准MATLAB的OO结构的一部分。
<在'调用者工作区中的'的名称和类的
evalin()</base'工作区变量
使用eg提取所分配变量的名称 regexp()
比较名称和类别。如果发生总匹配,即'base'工作空间中的变量正被同一类的新实例覆盖,请向用户询问 input()。如果用户选择保留现有对象,则通过 evalin('caller',...)用现有对象覆盖新实例。
classdef foo& handle
properties
end 方法 function obj = foo()%基本工作空间的变量名和大小 ws = evalin('base','whos');
%上次执行命令从窗口 fid = fopen([prefdir,'\history.m'],'rt');
while?feof(fid) lastline = fgetl(fid);
fclose(fid);
%比较名称和类别 outname = regexp(lastline,'\& [a-zA-Z] \w *(?=。*?匹配','一次');
if isempty(outname); outname ='ans'; end
%检查工作区中的变量是否具有相同的名称 idx = strcmp({ws.name},outname); %提问 if any(idx)&& strcmp(ws(idx).class,'foo')s = input(sprintf(['''%s''already already。'... ' n):'],outname),'s'); %用现有的实例覆盖新实例以保留它 if strcmpi(s,'n') obj = evalin('caller',outname);
$ b b 动作类:
%create class并将属性从default(true)更改为false 清除b
b = foo b =
foo与属性:检查:1
b.check = false b =
foo with properties: check:0
%避免覆盖b = foo 'b'已存在。将其替换为默认值? (y / n):n
foo与属性:检查:0
弱点(见上述):
仅适用于cmw行和脚本执行的命令,而不是函数(请参阅扩展到函数调用的链接)。
当前正则表达式在 a == b 。
危险,因为将打开潜在的安全威胁。即使输入使用正则表达式和字符串比较过滤,如果稍后再次查看代码,则构造可能会出现问题。
本文地址: &
扫一扫关注官方微信

我要回帖

更多关于 新铁锅怎么处理才能用 的文章

 

随机推荐