structure containing empty string arrays
1 次查看(过去 30 天)
显示 更早的评论
Hi, I'm using a structure to hold some text data. The fields of the structure are initialised as cells. However it seems that if the field is left empty then the value is converted to a double meaning I can't consistently index that value. Do I need to specifically test each value or is there a better way? Thanks
x=struct('a',{},'b',{});
x(1).a={'abc' 'bbc'};
x(1).b={'cbc'};
x(2).a={'dbc'};
for iloc=1:2
sprintf('%s,%s\n',...
x(iloc).a{:},...
x(iloc).b{:})
end
0 个评论
回答(2 个)
Azzi Abdelmalek
2013-2-19
编辑:Azzi Abdelmalek
2013-2-19
You can use
x=struct('a',{''},'b',{''});
Edit
n=10;
v=arrayfun(@(x) {''},1:n,'un',0)
a=struct('a',v,'b',v)
5 个评论
Azzi Abdelmalek
2013-2-19
编辑:Azzi Abdelmalek
2013-2-19
Ok, use this, it should work
n=10;
v=arrayfun(@(x) {''},1:n,'un',0)
a=struct('a',v,'b',v)
Sean de Wolski
2013-2-19
编辑:Sean de Wolski
2013-2-19
You should be able to use repmat() to create a cell array of empty strings:
C = repmat({''},10,10)
And then feed this into the struct() constructor.
1 个评论
Sean de Wolski
2013-2-19
@Tom, works fine for me:
C = repmat({''},10,10)
S = struct('C',C)
S(10,1).C = 'pi'
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!