MATLAB: Accessing variable names of cell arrays in another function

cell arraysMATLAB

Hi,
I have a function that has say 3 variables of different size (and this could vary for different input files):
EvVel = 301 x 12
EvAcc = 300 x 12
EvJerk = 299 x 12
I have assigned each of them to a cell array as I have to pass them to a function that generates reports:
my_array{1} = EvVel;
my_array{2} = EvAcc;
my_array{3} = EvJerk;
temp = repgen(my_array)
function repgen(my_array)
plot(my_array{1});
title('EvVel') %I am having problem accessing the variable name here as I am passing cell array
end

Best Answer

My recommendation would be to pack the data in a struct array with an additional field for the name/title:
my_struct = pack_in_struct(EvVel,EvAcc,EvJerk)
temp = repgen(my_struct);
function my_struct = pack_in_struct(varargin)
for i=numel(varargin):-1:1
my_struct(i).name=inputname(i);
my_struct(i).data=varargin{i};
end
end
function temp = repgen(my_struct)
for i=1:numel(my_struct)
figure;
plot(my_struct(i).data);
title(my_struct(i).name);
end
temp=...
end
Related Question