I have a struct variable which is parameters
with some other variables ex foo1
,foo2
. parameters
has 20 fields. ex. a,b,c,d,e ...
I want to save only 18 fields. I don't want to save parameters.a
and parameters.b
. So I want to save parameters
except the fields a
and b
and foo1
and foo2
. How can I do this?
Asked
Active
Viewed 485 times
0

Suever
- 64,497
- 14
- 82
- 101

toygan kılıç
- 422
- 4
- 16
1 Answers
1
You can either remove the fields before saving it with rmfield
.
tosave = rmfield(parameters, {'a', 'b'});
save(filename, '-struct', 'tosave');
or you could get a listing of all fields, remove the fields you don't want and then pass these to save
. This has the added benefit of not having to make a copy of the struct
.
% Get all fields of parameters
allfields = fieldnames(parameters);
% Remove the fields you don't want to keep
fields = allfields(~ismember(allfields, {'a', 'b'}));
% Now save the rest
save(filename, '-struct', 'parameters', fields{:});
Or as @excaza notes, you can craft a regular expression with the -regexp
flag to exclude the variables you don't want.

Suever
- 64,497
- 14
- 82
- 101
-
Or a regex: `save(filename, '-struct', 'data', '-regexp', '[^ab]');` Probably more of a pain than `ismember` though. – sco1 Oct 03 '16 at 20:59