字符串到变量名 MATLAB [英] String to variable name MATLAB

查看:67
本文介绍了字符串到变量名 MATLAB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我有一个变量 xa=2,然后我通过连接 'x' 和 'a' 来构造一个字符串,我怎样才能让这个新字符串的值为 2?

If for instance I have a variable xa=2, and then I construct a string by joining 'x' and 'a', how can I make this new string have the value 2?

xa=2;
var=strcat('x','a');

这样做的结果是var=xa,但我想要的是var=2.

The result of this is var=xa, but what I want is var=2.

谢谢

推荐答案

使用 eval():

var = eval(strcat('x','a'));

它将评估"字符串 'xa' 并将其转换为变量 xa 的值.

It will "evaluate" the string 'xa' and translate it to the value of the variable xa.

来源:MATLAB 文档

这篇关于字符串到变量名 MATLAB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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