作者celestialgod (攸藍)
看板MATLAB
標題Re: [討論] struct a.b([1:5]).c=1; 為什麼錯?
時間Mon Jun 29 17:47:06 2015
※ 引述《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);
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.248.7.74
※ 文章網址: https://www.ptt.cc/bbs/MATLAB/M.1435571229.A.1D9.html
推 s4300026: 推薦這篇文章~ 06/29 20:28
推 s4300026: 測試後發現不能用,原因是除了.c外,我還有其他field 06/29 21:18
→ celestialgod: 你只能考慮把其他field一起變成cell再用cell2struct 06/29 21:41
→ celestialgod: 一次轉 06/29 21:41
推 s4300026: 囧"" 06/30 08:40