Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

how to make a variable name from a string

Asked by xianghong

xianghong (view profile)

on 16 Aug 2013

2013a version

>> test='eye'
>> value = [5,5,3]
value =
       5     5     3
>> eval([sprintf(test) '= data+1']);
Index exceeds matrix dimensions.

How to fix this error? Thank you for helping!

0 Comments

xianghong

xianghong (view profile)

Products

No products are associated with this question.

4 Answers

Answer by kjetil87

kjetil87 (view profile)

on 16 Aug 2013
Edited by kjetil87

kjetil87 (view profile)

on 16 Aug 2013
Accepted answer

The error should not appear, your code looks to work exept for the part of 'data' , should this instead be value?

test='eye';
value=[1,2,3];
eval([sprintf(test) '=value+1'])
eye =
       2     3     4

The index exceeds matrix dimension could be related to that you have at some point declared a variable in your workspace ( or funciton or whatever ) with the name of eval.

try typing

which eval

it should return that eval is a built-in function, if it returns eval is a variable there is the reason for your error. If so just use

clear eval

2 Comments

kjetil87

kjetil87 (view profile)

on 16 Aug 2013

also the sprintf is not really needed. + also check out

http://www.mathworks.se/help/matlab/ref/assignin.html

xianghong

xianghong (view profile)

on 19 Aug 2013

when I tried this morning, it worked! Thank you so much! Have a great week!

Xianghong

kjetil87

kjetil87 (view profile)

Answer by xianghong

xianghong (view profile)

on 16 Aug 2013

This seems like to name the fields within a variable. If I just want to make a variable name from a string, how to do it? Thanks!

1 Comment

Walter Roberson

Walter Roberson (view profile)

on 16 Aug 2013

No, it is not just for creating field names within a variable. It shows a number of different alternatives to creating a variable from a string, recommending strongly that you use one of the alternatives instead of creating a variable dynamically. At the end of the FAQ it shows how to create a variable dynamically, for the rare case when you really need to do that. If you are not engaged in creating extensions to MATLAB itself, chances are that there is a better route than creating a variable dynamically.

xianghong

xianghong (view profile)

Answer by xianghong

xianghong (view profile)

on 16 Aug 2013

EDU>> test='eye';

EDU>> value=[1,2,3];

EDU>> eval([sprintf(test) '=value+1'])

Index exceeds matrix dimensions.

Looks like it still gave the error. What shall I do?

Thanks,

2 Comments

Walter Roberson

Walter Roberson (view profile)

on 16 Aug 2013

Please use the command

clear eval

and then try again

xianghong

xianghong (view profile)

on 19 Aug 2013

thank you so much! It worked this monday morning!

Have a nice day!

Xianghong

xianghong

xianghong (view profile)

Contact us