putting some matrix in a cell arrays

There are some Matrix, it's needed putting each of them in an array of a cell that the name of that array be equaled with the name of related matrix.
for example:
a=[1 2 3 4 5 3 2]; b=[4 3 5 67 8 8 7]; c=[3 3 2 4 65 7 8 89];
CELL{1,1}=a; CELL{1,2}=b; CELL{1,3}=c;
Now Question is: how could also be put names of above matrix in CELL?

 Réponse acceptée

Fangjun Jiang
Fangjun Jiang le 24 Sep 2011
If you want to have the names, maybe it's better to use structure.
Str.a=a;
Str.b=b;
Str.c=c;
Then you can use Names=fieldnames(Str) to get all the names.

3 commentaires

mohammad
mohammad le 24 Sep 2011
Fangjun Thanks
if it's needed the names of 600 .xls in a for-loop being put into a structure, how must it will be wrote. for example in each loop the names of .xls file equals to d(i).name (d=dir(*.xls) in a specified directory)
I tried Str.f=related matrix (f=d(i).name) in that loop but it saves only 'd(numel(d)).name' and data of that
Walter Roberson
Walter Roberson le 24 Sep 2011
Str(i).value = ... %the data itself
Str(i).name = d(i).name;
mohammad
mohammad le 24 Sep 2011
So nice, works perfect

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 24 Sep 2011
In theory the below should work:
var2name = @(varargin) arrayfun(@inputname, 1:nargin, 'Uniform',0);
Then
CELL = var2name(a,b,c);

13 commentaires

mohammad
mohammad le 24 Sep 2011
Thanks Walter
mohammad
mohammad le 24 Sep 2011
@Walter it's really absorbing that you can make 'name' of CELL{1,1} ,... the same name of variables
how must themself data be saved in CELL beside of the names of them?
mohammad
mohammad le 24 Sep 2011
so nice, with eval(CELL{,}) we could reach data of a,b,c but because of using loop that i explained on Fangjan's answer, a,b,c are not in workspace
Walter Roberson
Walter Roberson le 24 Sep 2011
Sorry, you question is confusing.
I suspect the answer is:
var2cell = @(varargin) [varargin; arrayfun(@inputname, 1:nargin, 'Uniform',0)];
Unfortunately my access to my MATLAB server is down this afternoon, so I cannot test this.
If it works, CELL{1,K} would be the variable value, and CELL{2,K} would be the variable name.
mohammad
mohammad le 24 Sep 2011
tanks Walter
let me try
mohammad
mohammad le 25 Sep 2011
Perfect i use your comment with under command for gathering all 600 matrix(i told on Fangjuns's answer)
cell = {};
cd(myfolder);
d=dir(*.xls);
for i=1:numel(d) %(numel(d)=600)
f=d(i).name;
r=xlsread(f); %(i use COM server code)
CELL=var2cell(r)
cell=[cell CELL];
end
Is this right?
Walter Roberson
Walter Roberson le 25 Sep 2011
Looks kind of useless to me. The name recorded would just be 'r' in each case.
The var2cell code was designed for the case where "the name of the array" is the variable name it is stored in.
And you have used cell as a variable name, destroying its meaning as an important call.
cd(myfolder);
d=dir('*.xls');
numfile = length(d);
CELL = cell(2,numfile);
for i = 1:numfile
f=d(i).name;
r=xlsread(f);
CELL{1,i} = r;
CELL{2,i} = f;
end
mohammad
mohammad le 25 Sep 2011
Thanks Walter
Fangjun Jiang
Fangjun Jiang le 25 Sep 2011
@mohammad, it seems to me that you went a long route to solve the problem. My understanding is that you have 600 Excel files, you want to read the data for each file and assign the data to a variable that has the same name as the Excel file, and you want to have this name in a string. Use the dynamic field name of a structure can achieve this.
For example, if you have file MyData.xls, you will be able to get
Files=dir('*.xls');
FileName=Files(1).name; %FileName will be 'MyData.xls'
VarName=strrep(FileName,'.xls','');
Str.(VarName)=xlsread(FileName); %or use COM server
Then, you'll have Str.MyData containing the data from MyData.xls, Str.OtherData from OtherData.xls, etc.
Is that what you want?
mohammad
mohammad le 25 Sep 2011
Exactly it is! Vao! really nice and speedy
Thanks a lot Fangjun
Walter Roberson
Walter Roberson le 25 Sep 2011
It would be a good idea to instead use
VarName = genvarname(strrep(FileName,'.xls',''));
in case the file name started with a number or had a character which is not allowed for MATLAB variable names.
Fangjun Jiang
Fangjun Jiang le 25 Sep 2011
Agree! That's a good programming practice. +1
mohammad
mohammad le 26 Sep 2011
So thanks Walter and Fangjun

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by