Re: [討論] struct a.b([1:5]).c=1; 為什麼錯?

作者: celestialgod (天)   2015-06-29 17:47:06
※ 引述《s4300026 (s4300026)》之銘言:
: 如題
: 我想要做到
: a.b(1).c = 1
: a.b(2).c = 1
: a.b(3).c = 1
: a.b(4).c = 1
: a.b(5).c = 1
: 就很順手的打
: a.b([1:5]).c=1;
: 然後就得到
: Insufficient outputs from right hand side to
: satisfy comma separated list expansion on left hand
: side. Missing [] are the most likely cause.
: 該怎麼改成正確呢?
我不知道有沒有更好的方法,我找到的方式是這樣
a = struct('b', []);
a.b = cell2struct(cellfun(@(x) 1, cell(5,1), ...
'uniformoutput', false), {'c'}, 2);
作者: s4300026 (s4300026)   2015-06-29 20:28:00
推薦這篇文章~測試後發現不能用,原因是除了.c外,我還有其他field
作者: celestialgod (天)   2015-06-29 21:41:00
你只能考慮把其他field一起變成cell再用cell2struct一次轉
作者: s4300026 (s4300026)   2015-06-30 08:40:00
囧""

Links booklink

Contact Us: admin [ a t ] ucptt.com