Merging several mat files in a row order

Hi everyone,
I have 65 mat files with different names and each mat file is 1x1 struct, contains information as longitude, latitude, year(335x1), month(335x1) etc.
So how can i combine them as a single mat file which should have first row as a information of first mat file, second row as a information of second mat file, until 65 rows of each mat file?
I would be very pleased if you have any idea about how to do it?

Answers (1)

You did not explain, how a list of the files names can be obtained. I guess, that they can be found in the same folder:
Folder = 'C:\your\folder';
Files = dir(fullfile(Folder, '*.mat'));
DataC = cell(1, numel(Files));
for iFile = 1:numel(Files)
aFile = fullfile(Folder, Files(iFile).name);
DataC{iFile} = load(aFile);
end
Data = cat(2, DataC{:});

4 Comments

Thank you Jan, seems like it merges the structs into structs again with mat files name. which looks like this;
DataC{1,3}.astoria and this contains DataC{1,3}.astoria.longitude like this and it continues as the name of mat files like DataC{1,4}.betio and to reach it's data, i also need to do DataC{1,4}.betio.longitude
That's why it give error at concatenation as names of fields in structure arrays being concatenated do not match. Concatenation of structure arrays requires that these arrays have the same set of fields.
Is it possible to merge each of them by rows like being
longitude latitude datetime observation
103.650000000000 1.23333300000000 334x1 datetime 334x1 double
103.750000000000 1.16666700000000 335x1 datetime 335x1 double
. . . .
or names of the mat files can be at first column?
I hope the way i am trying to head is clear.
Of course I cannot guess, what the contents of your MAT files is. Now it is clear, that the files contains structs with a field name "astoria" and "betio" with the subfield "longitude". But where does "latitude", "datetime" and "observation" come from? It is no save strategy, if I guess this.
What is the wanted output? You want "a single MAT file", but your example looks like a table. Should it be a text file?
files contains structs with a field name and with subfields of longitutde latitude datetime and observation as well. Expected output is having one mat file that each file with its subfields in a row, so that i can just call with the row order.
Has it become more clear?
What are "subfields in a row"? Fields do not have an orientation. What is "calling with the row order"?
I assume, you want to create either a struct array, or to write a text file containing rows.

Sign in to comment.

Categories

Asked:

on 11 Jul 2021

Commented:

Jan
on 15 Jul 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!