renaming a cell

29 次查看(过去 30 天)
ricco
ricco 2011-12-1
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

回答(2 个)

David Young
David Young 2011-12-1
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 个评论
ricco
ricco 2011-12-1
I've tried this, it says that it is an invalid field name! any advice?
David Young
David Young 2011-12-1
Works for me! I've put example code in my answer to show how it goes.

请先登录,再进行评论。


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

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by