remove specific array in struct

2 ビュー (過去 30 日間)
Xuande Zhang
Xuande Zhang 2019 年 1 月 7 日
コメント済み: Xuande Zhang 2019 年 1 月 7 日
Hey guys,
I'm stuck with one simple issue. I am practicing on 'struct' now. As practice, I created a stuct 'account' with fields 'owner', 'balance', and 'number' which is 1x3 array.
Then I was trying to creat the second account 'account(2)', however I accidentally typed 'account(2).name' which made a 1x4 array.
Tried rmfield, but failed:"Subscripted assignment between dissimilar structures."
Can someone help? Thanks
account(2) = rmfield(account(2),field)
  1 件のコメント
nanren888
nanren888 2019 年 1 月 7 日
編集済み: nanren888 2019 年 1 月 7 日
A struct array has the same fields for all elements.
You cannot remove a field from only account(2)
try typing
account

サインインしてコメントする。

採用された回答

nanren888
nanren888 2019 年 1 月 7 日
account = struct('owner','Harry','balance',0,'number',42)
account =
struct with fields:
owner: 'Harry'
balance: 0
number: 42
>> account(2) = struct('name','Harry','balance',0,'number',42)
Subscripted assignment between dissimilar structures.
>>Not sure how you created it
>> account(2).name = 'Harry'
account =
1×2 struct array with fields:
owner
balance
number
name
>> account
account =
1×2 struct array with fields:
owner
balance
number
name
>> account = rmfield(account,'name')
account =
1×2 struct array with fields:
owner
balance
number
or maybe
account(2).owner = account(2).name; # first
  1 件のコメント
Xuande Zhang
Xuande Zhang 2019 年 1 月 7 日
Thanks for your help! It worked.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStructures についてさらに検索

タグ

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by