フィルターのクリア

Remove all fields from a struct except if

7 ビュー (過去 30 日間)
Fernando
Fernando 2012 年 7 月 17 日
Hi,
I have a struct mk with fields mk.id, mk.x and mx.z. There are 800 of each, so I have mk(1).id, ..., mk(800).id, and the same for x and z.
I need to do several calculations that require loading only one of these at the time, for a given parameter. This is, I have a function that takes one argument, say i, and I would like to load the fields that correspond to that i. Something such as load('input','mk(i)'), to get mk(i).id, mk(i).x and mk(i).z. This doesn't work, and load(fullfile('input.mat),'mk(i)') doesn't work either (with i replaced by a number). I receive a message saying "Warning: Variable 'mk(1)' not found".
Any suggestions? An alternative would be to load everything and delete all but mk(i).id, mk(i).x and mk(i).z, but I have no idea how to remove all but those fields.
Thanks,
Fernando

採用された回答

per isakson
per isakson 2012 年 7 月 17 日
編集済み: per isakson 2012 年 7 月 17 日
MATFILE is the closest there is to your requirement. And it is a rather new feature. I use R20012a.
N = 8;
mk = struct( 'id' , num2cell(transpose(1:N)) ...
, 'x' , num2cell(rand(N,1)) ...
, 'y' , num2cell(randn(N,1)) );
save( 'mk.mat', 'mk' )
mo = matfile( 'mk.mat' ); % creates an object
mo.mk(7,1)
ans =
id: 7
x: 0.8407
y: 0.4882
.
However, there are problems with MATFILE, see: Writing to matfile in a loop
  1 件のコメント
Fernando
Fernando 2012 年 7 月 17 日
Thanks! I didn't know about this.

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

その他の回答 (0 件)

カテゴリ

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