How to merge data in mat file?
1 view (last 30 days)
Show older comments
I have a mat file 'pot1.mat'.
In the mat file, some rows has no data like this image.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/146059/image.jpeg)
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.
0 Comments
Answers (2)
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!
0 Comments
Walter Roberson
on 26 Dec 2024
load pot1
newpot = pot(~arrayfun(@(S)isempty(S.tr), pot));
save('newpot.mat', 'newpot')
0 Comments
See Also
Categories
Find more on Low-Level File I/O in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!