Matlab:从命令窗口重命名工作区元素? [英] Matlab: renaming workspace elements from command window?

查看:2461
本文介绍了Matlab:从命令窗口重命名工作区元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Matlab的GUI允许我通过右键单击元素并选择重命名选项来重命名工作空间中的任何元素。是否可以从命令窗口执行此操作?

The GUI of Matlab allows me to rename any element in the workspace by right-clicking on the element and selecting the 'rename' option. Is it possible to do this from the command window as well?

推荐答案

这些是你可以轻松测试自己的东西,应该这样做。这是学习,发现的最好方法。

These are things you can easily test for yourself, and you should do so. That is the best way to learn, to discover.

无论如何,答案是否定的,你不能从命令窗口以这种方式更改变量名。命令窗口主要用于键盘输入。

Regardless, the answer is no, you cannot change a variable name in that way from the command window. The command window is mainly for keyboard input only.

编辑:问题显然是关于通过命令窗口中的命令进行更改,而不是通过鼠标完成。 (为什么不告诉我们前面?)

The question was apparently about doing that change by a command in the command window, not to be done via a mouse. (Why not tell us that up front?)

没有明确的命令做这样的重命名。但是,没有什么阻止你自己写。例如...

There is no explicit command that does such a rename. However, nothing stops you from writing it yourself. For example...

function renamevar(oldname,newname)
% renames a variable in the base workspace
% usage: renamevar oldname newname
% usage: renamevar('oldname','newname')
%
% renamevar is written to be used as a command, renaming a single
% variable to have a designated new name
%
% arguments: (input)
%  oldname - character string - must be the name of an existing
%          variable in the base matlab workspace.
%
%  newname - character string - the new name of that variable
%
% Example:
% % change the name of a variable named "foo", into a new variable
% % with name "bahr". The original variable named "foo" will no
% % longer be in the matlab workspace.
%
% foo = 1:5;
% renamevar foo bahr

% test for errors
if nargin ~= 2
  error('RENAMEVAR:nargin','Exactly two arguments are required')
elseif ~ischar(oldname) || ~ischar(newname)
  error('RENAMEVAR:characterinput','Character input required - renamevar is a command')
end

teststr = ['exist(''',oldname,''',''var'')'];
result = evalin('base',teststr);
if result ~= 1
  error('RENAMEVAR:doesnotexist', ...
    ['A variable named ''',oldname,''' does not exist in the base workspace'])
end

% create the new variable
str = [newname,' = ',oldname,';'];
try
  evalin('base',str)
catch
  error('RENAMEVAR:renamefailed','The rename failed')
end

% clear the original variable
str = ['clear ',oldname];
evalin('base',str)

这篇关于Matlab:从命令窗口重命名工作区元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆