Assign multiple fields to a Struct-Array

8 ビュー (過去 30 日間)
Vincent
Vincent 2011 年 9 月 9 日
hi, I'm new to struct Arrays and I've got the following problem:
filelist(:).names = upper(filelist(:).names);
somehow I cannot figure out how to tell matlab to assign each result of the upper function to one entry in the filelist. I'm sure that there's a simple solution but I understand how until now
Thanks!

回答 (3 件)

Fangjun Jiang
Fangjun Jiang 2011 年 9 月 9 日
filelist(1).names='a';
filelist(2).names='b';
a=upper({filelist.names});
[filelist.names]=deal(a{:})
  2 件のコメント
Andrei Bobrov
Andrei Bobrov 2011 年 9 月 9 日
+1
[filelist.names]=a{:}
Fangjun Jiang
Fangjun Jiang 2011 年 9 月 9 日
Thanks, andrei! deal or no deal!

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


Andrei Bobrov
Andrei Bobrov 2011 年 9 月 9 日
for j1 = numel(filelist):-1:1
filelist(j1).names=upper(filelist(j1).names);
end
variant with arrayfun and others
filelist = ...
cell2struct(arrayfun(@(x)upper(x.names),filelist,'un',0),fieldnames(filelist));

Vincent
Vincent 2011 年 9 月 9 日
Is there also a solution avoiding the loop?
  3 件のコメント
Vincent
Vincent 2011 年 9 月 9 日
less lines, nicer code and isn't it often faster to use vecorization (if possible)
anyway thank you very much for your nice answers!
Fangjun Jiang
Fangjun Jiang 2011 年 9 月 9 日
So, is my answer above what you are looking for?

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by