Reputation: 27
To combine data, I want to load one data as the base assign the data to another variable (e.g. naming it as base_data). Then load in another data and loop through all of its fields. If the current field doesn't exist in the base data, add the field to the base data. (e.g. base_data.fieldname = data.fieldname). Then I want to save the base_data to file. Can I know the commands to do this in Matlab?
Upvotes: 1
Views: 1172
Reputation: 12683
Use dynamic field names:
base_data = load('A.mat');
B = load('B.mat');
fn = fieldnames(B);
for ii=1:length(fn)
fieldname = char(fn(ii));
if ~isfield(base_data,fieldname)
base_data.(fieldname) = B.(fieldname);
end
end
save('base_data','base_data')
Upvotes: 1