renaming a cell

29 views (last 30 days)
ricco
ricco on 1 Dec 2011
I have a 1x4 cell called 'naming' which contain names of 4 data sets e.g. 'data1', 'data2' etc...
I have another 1x4 cell called 'File1' which contains 4 cells e.g. 19x1 cell, 19x1 cell, 19x1 cell, and a 18x1 cell.
All I would like to do is to use the names in 'naming' as the name of each cell in 'File1'. So, instead of having 19x1 cell etc... I would have 'data1' and so on. It seems straight forward but I attempted to use
New_File=struct(naming(1),File1(1)...)
But it doesnt work as it requires a string input, which I thought 'naming(1)' would be a string?
cheers

Answers (2)

David Young
David Young on 1 Dec 2011
naming(1)
is a cell, but
naming{1}
is a string.
EDIT: added example
>> naming = {'data1', 'data2', 'data3', 'data4'};
>> File1 = {rand(19,1), rand(19,1), rand(19,1), rand(18,1)};
>> New_File = struct(naming{1}, File1{1}, naming{2}, File1{2}, naming{3}, File1{3}, naming{4}, File1{4});
>> New_File
New_File =
data1: [19x1 double]
data2: [19x1 double]
data3: [19x1 double]
data4: [18x1 double]
You can simplify this further. Instead of calling struct, use
New_File = cell2struct(File1, naming, 2);
  2 Comments
ricco
ricco on 1 Dec 2011
I've tried this, it says that it is an invalid field name! any advice?
David Young
David Young on 1 Dec 2011
Works for me! I've put example code in my answer to show how it goes.

Sign in to comment.


Fangjun Jiang
Fangjun Jiang on 1 Dec 2011
>> s=cell2struct(File1,naming,2)
s =
data1: [19x1 double]
data2: [19x1 double]
data3: [19x1 double]
data4: [18x1 double]

Categories

Find more on Structures in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!