How to merge data in mat file?

1 view (last 30 days)
MIHYUN
MIHYUN on 11 Nov 2014
Answered: Walter Roberson on 26 Dec 2024
I have a mat file 'pot1.mat'.
In the mat file, some rows has no data like this image.
So, I want to save the row in which the data exists to other mat file.
Exactly, pot1(a).tr(:,1).
I don't have the idea that How to save.
I'll appreciate if you could help me.
Thank you.

Answers (2)

Shuba Nandini
Shuba Nandini on 26 Dec 2024
Hi,
It is my understanding that you want to retrieve rows from the MAT file where data exists and save them to another MAT file.
To save only the rows of pot1(a).tr(:,1) where data exists to another MAT file, you can run the below code to create a new MAT file:
% Load the MAT file
fileData = load('pot1.mat');
% Get the field name
varNames = fieldnames(fileData);
pot1 = fileData.(varNames{1});
% Initialize a cell array to store data
nonEmptyData = [];
% Loop through each element in pot1
for a = 1:length(pot1)
if ~isempty(pot1(a).tr) % Check if the entry is not empty
% Extract the first column of the data
firstColumn = pot1(a).tr(:,1);
% Append to the nonEmptyData array
nonEmptyData = [nonEmptyData; firstColumn];
end
end
% Save the non-empty data to a new MAT file
save('nonEmptyPot1.mat', 'nonEmptyData');
For more information on how to save the data into the MAT file, kindly refer to the following MathWorks documentation:
I hope this helps!

Walter Roberson
Walter Roberson on 26 Dec 2024
load pot1
newpot = pot(~arrayfun(@(S)isempty(S.tr), pot));
save('newpot.mat', 'newpot')

Community Treasure Hunt

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

Start Hunting!