Take the 2-minute tour ×

I am trying to use eval to assign a cellValue to a field in a struct. The struct has been pre-initialized. I am getting a horzcat error that I can't remove. Can you please help me out here. Also, I know using eval is not recommended. Can you suggest a better alternative to it that can be used here. cellValue is a big cell with almost 200,000 rows and this step would be run repeatedly. So vectorization and efficiency is needed. Thanks!

varA = struct('Raw', -1, 'ttm', -1, 'USD', -1) ;
cellValue = { 'AAPL' [10010] 'abc' ;  
              'AAPL' [10020] 'abc' ; 
              'AAPL' [10030] 'abc' } ;

eval([varA '.Raw = ' cellValue ]) %gives horzcat ERROR

% Desired result from varA.Raw = cellValue

varA = 
    Raw: {3x3 cell}
    ttm: -1
    USD: -1
share|improve this question
Judging by your own answer, you actually wanted to write somevar=struct(...);varA='somevar';... otherwise your proposed solution would still fail. –  Jonas Heidelberg Aug 24 '11 at 22:19
... and in your second code block, it wouldn't be the variable varA which contains that data, but the variable whose name is stored in the string variable varA. –  Jonas Heidelberg Aug 24 '11 at 22:22

3 Answers 3


You can add another nesting level, and use dynamic field names to reference the variable names:

varName = 'A';
vars.(varName).Raw = value;

Now vars is a structure whos fields are also structures. So you would access them as:

>> vars
vars = 
    A: [1x1 struct]
    B: [1x1 struct]
    C: [1x1 struct]

>> vars.A
ans = 
    Raw: {3x3 cell}
    ttm: -1
    USD: -1

>> vars.A.Raw
ans = 
    'AAPL'    [10010]    'abc'
    'AAPL'    [10020]    'abc'
    'AAPL'    [10030]    'abc'
share|improve this answer
During runtime, varA keeps changing. So you can have varA,varB,varC...etc in any order. This is run being inside a loop. –  Maddy Jul 29 '11 at 18:39
@Maddy: see my edit above. Although a cleaner approach would be to use an array of structures (use indices instead of variable names to refer to the various components)... –  Amro Jul 29 '11 at 18:54
@Amro..this would also not work. Actually varA, varB etc example was wrong. The variables names are actually ni, equity, cogs etc. Everytime I get a big matrix with 1,2,3 or 4 of these names in it in 10000s of rows. What you are suggesting may requires me to change my global variable design itself! Can you please suggest any other assignment approach? Thanks! –  Maddy Jul 29 '11 at 19:16
@Maddy: global variables and EVALs, you are probably doing it wrong :) blogs.mathworks.com/loren/2006/01/04/more-in-eval, mathworks.com/support/tech-notes/1100/1103.html –  Amro Jul 29 '11 at 19:20
@Amro... Please comment on a 30,000 feet approach to this design problem. You have 300 functions. They use variety of data: say 80 data items (matrices) and the names of these matrices are not sequential (i.e. say 'ni' 'equity' etc and NOT mat1, mat2 etc.). OOP approach is not possible as these 300 functions are grouped into 7 categories. Dataitems are used across all categories. By making them global, you can easily use them anywhere. Inside function you assign global values to local matrices and do your stuff. What else would work? OOP approach is not possible for now. Thanks! –  Maddy Jul 29 '11 at 19:35

This expression:

[varA '.Raw = ' cellValue ]

is trying to concatenate a struct, a string, and a cell together into one matrix, which is not possible. That's why you get a horzcat error. What I'm not sure of is why you are using eval. This achieves your desired result:

varA.Raw = cellValue;

If you really want that in an eval, you could do

eval('varA.Raw = cellValue;')
share|improve this answer
up vote 0 down vote accepted

well this works:

str = strcat(varA,'.Raw = cellValue ')  
%varA changes every time. cellValue name is constant.

share|improve this answer

Your Answer


