Putting the result from eval into a cell array

28 views (last 30 days)
Got the piece of code below. Basically, when I run it I get x_1 all the way to x_600 so i get 600 outputs I would like to put all of these into a cell array does anyone know how to do this cause I've failed at every attempt?
for i=1:600
G = [ F(i)+1 , F(i+1) ];
a = unwrap(B);
eval(['x_' num2str(count) ' = a;']);

Answers (2)

Stephen23 on 21 Jul 2019
Edited: Stephen23 on 21 Jul 2019
N = 600;
C = cell(1,N); % preallocate.
for k = 1:N
G = [F(k)+1,F(k+1)]; % you never use G anywhere in your code...
C{K} = unwrap(B); % where is B defined?
"...I've failed at every attempt"
Because you picked a complex, slow, inefficient approach (using eval to create lots of variables), as opposed to simply using a cell array in the first place.
TADA on 21 Jul 2019
Edited: TADA on 21 Jul 2019
If you store data sets in arrays instead of indexed variables, you get only benefits:
  • Less code which is more readable:
x{i} = unwrap(B(G(1):G(2),:)/2^15*pi)*2^15/pi;
% instead of
count = count+1;
a = unwrap(B(G(1):G(2),:)/2^15*pi)*2^15/pi;
eval(['x_' num2str(count) ' = a;']);
  • No need to maintain a variable which counta your variables, simply use size(x) or numel(x)
  • No workspace spamming with 600 variables, theres only one cell array,matrix,table,etc. So you can actually see what variables you have in your workspace window.
  • Indexing performs way better than eval, several orders of magnitude better in fact
a = 1:100;
for i = 1:10000;
x{i} = a;
Elapsed time 0.002012 seconds
for i = 1:10000;
eval(['x_' num2str(i) ' = a;']);
Elapsed time is 3.508633 seconds

Sign in to comment.

TADA on 21 Jul 2019
please please please don't use eval to access varriable names
eval is not built for that, however, for some reason the first thing that new programmers of any scripting language learn is to use eval which results in code which is confusing, performs poorly and is impossible to decypher and maintain
remember the rule, eval is evil.
if you remember that and only result to using eval when you realy have to (i.e. never!), your programing would improve dramatically.
the good practice in that case would be to use matrices to store your data instead of separate variables (i.e X1 X2 X3), or if they are different sizes a cell array
% same size:
x = zeros(3, 100);
x(1,:) = 1:100;
x(2,:) = 101:200;
x(3,:) = 201:300;
% different sizes:
x = {};
x{1} = 1:100;
x{2} = 1:1000;
x{3} = 1:10000;
Now, if that piece of code was given to you by some blackbox script written by someone else and it requires hundreds of hours of rewriting, I agree the correct way to handle this would be to use eval and export the data into a reasonable format:
% code you got from someone else
X1 = 1:100;
X2 = 1:1000;
X3 = 1:10000;
% code that accesses these variables and fixes that
c = {};
for i = 1:3
c{i} = eval(['X' num2str(i)]);


Find more on MATLAB Report Generator in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by