Matlab:从命令窗口重命名工作区元素?

发布于 2024-11-06 15:18:37 字数 71 浏览 0 评论 0 原文

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪是无色的血 2024-11-13 15:18:37

这些是您可以轻松亲自测试的事情,并且您应该这样做。这是学习、发现的最佳方式。

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

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

没有明确的命令可以执行此类重命名。但是,没有什么可以阻止您自己编写它。例如...

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)

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.

Edit: 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)
薄凉少年不暖心 2024-11-13 15:18:37

您可以在命令窗口中重命名变量,如下所示:

%# create a variable
a = 3;

%# rename a to b
b = a;clear('a');

编辑

如果要将变量重命名为存储在字符串中的另一个变量,可以使用分配

a = 3;
newVarName = 'b';
assignin('base',newVarName,a);
clear('a') %# in case you want to get rid of the variable a

You can rename variables in the command window as follows:

%# create a variable
a = 3;

%# rename a to b
b = a;clear('a');

EDIT

If you want to rename your variable to another variable stored in a string, you can use ASSIGNIN

a = 3;
newVarName = 'b';
assignin('base',newVarName,a);
clear('a') %# in case you want to get rid of the variable a
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文