Performing Action to ALL 'struct' Variables
4 次查看(过去 30 天)
显示 更早的评论
Is there a way to perform the same action on several variables of type 'struct'?
In my case, I have 30 separate 'struct' variables (with non-logical names), and each of them contains a single vector of 200 elements. I only want to deal with the second half of these elements so I need something like:
struct_name = struct_name.data(100:end)
But need to keep the name of the struct variable intact. Does anyone have any suggestions?
I hope this made sense!
0 个评论
采纳的回答
Matt Fig
2011-5-19
% Create several structures with different names...
structn.dat = 1:10;
structm.dat = 1:10;
structk.dat = 1:10;
% Now edit the data.
save mystructs structn structm structk
X = load('mystructs');
F = fieldnames(X);
for ii = 1:length(F)
X.(F{ii}).dat = X.(F{ii}).dat(5:10);
end
% Check
X.structm.dat
8 个评论
Walter Roberson
2011-5-19
编辑:per isakson
2018-2-25
SFN = fieldnames(MyStruct);
for K = 1 : length(SFN)
var_name = SFN{K};
VFN = fieldnames(MyStruct.(var_name));
for N = 1 : length(VFN)
this_field = VFN{N};
if isvector(MyStruct.(var_name).(this_field)) & ...
length(MyStruct.(var_name).(this_field)) > 100
MyStruct.(var_name).(this_field) = MyStruct.(var_name).(this_field)(1:100);
end
end
end
更多回答(1 个)
Fangjun Jiang
2011-5-19
Would this help?
a(200).b=1;
a(200).c=0;
NewStruct=a(100:end);
Or, Do you mean keep the struct variable name, not the field names:
a(1:99)=[];
Now I think you mean this:
a.Data=1:200;
a.Data(1:100)=[]
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Structures 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!