Load and Append Data files
조회 수: 12 (최근 30일)
이전 댓글 표시
Hello,
I am trying to load and append several matlab files. Each .mat file has 2 1x1 structures, a Data struct (has 12 fields, each with 1x50 cells), and a Settings struct (10 fields).
I'd like to load 2 (or more) .mat files, and compare the settings. I've found the STRUCTCMP function, which works well. If the settings are the same, I'ld like to append the Data structs for the two mat files so that the fields stay the same, but now each has 1x100 cells.
I have a few problems with my code. First, when I load the file, I'd like to rename each structure so that data1 = data; and settings1 = settings; but I haven't figured out how to do this in the loop. Second, I'm not sure how to increase the size of the structure array, data.
%%create variable names for the structures
dataNum = input('How many data files to analyze? ')
for a = 1:dataNum
data{a} = strcat('data',num2str(a));
Settings{a} = strcat('Settings',num2str(a));
end
%%load the data files, check to make sure they have the same settings
for i = 1:dataNum
PData = input('What is the filename? ','s');
load(PData) % load file
Settings(i) = settings; %rename settings structure, eg. settings1, settings2, etc.
if i >1
if ~STRUCTCMP(settings1,settings(i) %check to make sure they have the same settings
fprintf('Error: data files have different settings')
settings(i)
break; %stop if this is not true
end
end
data(i) = Data; %rename the data structure, eg. data1, data2, etc.
i = i+1;
end
%%append the data structures together
data = [data1 data2]; %# create a structure array of the data
names = fieldnames(data); %# get the field names
cellData = cellfun(@(f) {vertcat(data.(f))},names); %# collect field data into
%# a cell array
data = cell2struct(cellData,names); %# convert the cell array into a structure
Thanks for your help, this is my first analysis script, and my first time posting on the forum. Christina
댓글 수: 0
답변 (1개)
Sean de Wolski
2014년 10월 23일
Rather than loading directly with
load filename
which just creates some variables magically (we use the term "poofing"), instead, load into a structure:
S = load('filename.mat');
You can index into S directly
S.data
S.settings
Now just have to separate loaded structures S1 and S2, each with two fields containing your other structures. If everything works out with settings, concatenate them directly:
Scat = struct('data',[S1.data S2.data],'settings',[S1.settings, S2.settings])
Welcome to MATLAB Answers!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!