将数组变量名称转换为字符串

时间:2019-04-02 09:19:51

标签: arrays string matlab

如何在matlab中将数组或矩阵中的变量名称转换为字符串,例如,如果我有:

variable1 = 2;
variable2 = 5;
variable3 = 6;

variables_array = {
{[variable1 variable2 variable3]}
{[variable3 variable2 variable1]}
}

如何显示类似字符串(变量名,而不是值) [variable3 variable2 variable1]

1 个答案:

答案 0 :(得分:1)

仅是可视化,Ander和Wolfie的建议以及如何使用它,这是小代码段(另请参阅如何generate field names from variables):

% Variable names (May be dynamically created by superordinate system?)
varNames = {'x', 'yy', 'zzzZZZ'};

% Values created by superordinate system
values = [1, 4.5, 22.322];

% Mimic variable and value generation of superordinate system
for k = 1:numel(varNames)
  variable = varNames{k};
  value = values(k);

  % Superordinate system should store variables and values in struct.
  sysStruct.(variable) = value;
end

% Content of struct
sysStruct

这将给出以下输出,我想正是您想要的!!

sysStruct =

  scalar structure containing the fields:

    x =  1
    yy =  4.5000
    zzzZZZ =  22.322