load .mat files in loop

I want to load .mat files saved in a specific foldar and perform some operations on each one of them and then save the answer in an array.
  1. Load .mat files in sequence with file names
S_out_02-10-2024 16-55.mat; S_out_02-10-2024 16-56.mat;S_out_02-10-2024 16-57.mat;S_out_02-10-2024 16-58.mat;........
content of each file is 3x6 double matrix
2. Do some calculations on data received from each file.
3. Save the output from calculation into another .mat file with 1 column and rows as output of each calculations.
Please suggest an effcient matlab code for the same.

1 Comment

Stephen23
Stephen23 on 11 Feb 2024
Edited: Stephen23 on 11 Feb 2024
"Please suggest an effcient matlab code for the same."
I presume that those digits represent dates and that by "in sequence" you actually mean in chronological order. If the dates had been given in an ISO 8601 date format (i.e. largest to smallest units) then you could trivially SORT them into chronological order.
However, because the date units are all reversed (i.e. smallest to largest) you make this task much more difficult. You will have to do something like:
  • use DIR and parse the filenames into some kind of date representation (e.g. DATETIME) and SORT that.
  • build the filenames from lists of all known units that exist in the filenames.
  • something else...
Either way it will be more effort compared to if the filenames had been more carefully defined. Better data design makes code simpler, more robust, and much more efficient.

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 11 Feb 2024
Edited: Stephen23 on 11 Feb 2024
Create some fake data files (it is much better when you provide some data files for us to work with):
X=1; save 'S_out_02-10-2024 16-55.mat' X
X=2; save 'S_out_02-10-2024 16-56.mat' X
X=3; save 'S_out_02-10-2024 16-57.mat' X
X=4; save 'S_out_02-10-2024 16-58.mat' X
X=5; save 'S_out_01-11-2024 16-58.mat' X
Get filenames:
P = '.'; % absolute or relative path to where those files are saved
S = dir(fullfile(P,'S_out*.mat'));
{S.name} % note: NOT in chronological order!
ans = 1×5 cell array
{'S_out_01-11-2024 16-58.mat'} {'S_out_02-10-2024 16-55.mat'} {'S_out_02-10-2024 16-56.mat'} {'S_out_02-10-2024 16-57.mat'} {'S_out_02-10-2024 16-58.mat'}
Sort into chronological order:
D = datetime({S.name},"InputFormat","'S_out'_d-M-y H-m'.mat'");
[~,X] = sort(D);
S = S(X);
{S.name} % now in chronological order
ans = 1×5 cell array
{'S_out_02-10-2024 16-55.mat'} {'S_out_02-10-2024 16-56.mat'} {'S_out_02-10-2024 16-57.mat'} {'S_out_02-10-2024 16-58.mat'} {'S_out_01-11-2024 16-58.mat'}
Process & save:
N = numel(S);
Z = nan(N,1);
for k = 1:N
F = fullfile(S(k).folder,S(k).name);
A = load(F);
M = A.X; % change "X" to whatever name your matrix has
Z(k) = sqrt(M); % do your calculations on matrix M
end
save('output.mat','Z')
The output:
disp(Z)
1.0000 1.4142 1.7321 2.0000 2.2361

6 Comments

Hi,
I this is my code. I am getting error as: Unrecognized field name "X". (Line 61)
I have also attached the data files and the function used.
clear all
% Define the folder containing the .mat files
folder = 'C:\Users\Laborbenutzer\Desktop\Tomography Test Akriti\S_out';
% Specify the pattern for .mat file names
filePattern = fullfile(folder, 'S_out_*.mat');
% Get the list of .mat files in the folder
matFiles = dir(filePattern);
{matFiles.name} % note: NOT in chronological order!
D = datetime({matFiles.name},"InputFormat","'S_out'_d-M-y H-m'.mat'");
[~,X] = sort(D);
matFiles = matFiles(X);
{matFiles.name} % now in chronological order
N = numel(matFiles);
Z = nan(N,1);
for a = 1:N
F=fullfile(matFiles(a).folder, matFiles(a).name);
S=load(F);
S_out = S.X;
S_out=[[1 1 1 1 1 1]; S_out];
% Pauli matrices
S0 = [1 0;0 1];
S1 = [0 1;1 0];
S2 = [0 -1i;1i 0];
S3 = [1 0;0 -1];
S_all = {S0,S1,S2,S3};
Sbase.H = [1;1;0;0];
Sbase.D = [1;0;1;0];
Sbase.V = [1;-1;0;0];
Sbase.A = [1;0;-1;0];
Sbase.R = [1;0;0;1];
Sbase.L = [1;0;0;-1];
Sto_in=[Sbase.H,Sbase.D,Sbase.V,Sbase.A,Sbase.R,Sbase.L];
MLE_data.N_inpS = size(Sto_in,2);
Rho_out = zeros(2,2,MLE_data.N_inpS);
MLE_data.Rho_in = zeros(2,2,MLE_data.N_inpS);
% Measurement operators (POVMs)
MLE_data.N_meas = 6;
eigV(:,1) = 1/sqrt(2)*[1; 1];
eigV(:,2) = 1/sqrt(2)*[-1; 1];
eigV(:,3) = 1/sqrt(2)*[-1i; 1];
eigV(:,4) = 1/sqrt(2)*[-1i; -1];
eigV(:,5) = [1; 0];
eigV(:,6) = [0; 1];
MLE_data.povm = zeros(2,2,MLE_data.N_meas);
for m=1:MLE_data.N_meas % Loop of all POVMs
MLE_data.povm(:,:,m) = eigV(:,m)*eigV(:,m)';
end
for j=1:MLE_data.N_inpS
MLE_data.Rho_in(:,:,j) = (S0*Sto_in(1,j) + S1*Sto_in(2,j) + S2*Sto_in(3,j) + S3*Sto_in(4,j))/2;
Rho_out(:,:,j) = (S0*S_out(1,j) + S1*S_out(2,j) + S2*S_out(3,j) + S3*S_out(4,j))/2;
for l=1:MLE_data.N_meas % Loop of all input states
MLE_data.RelFreqBaseOut(l,j) = trace(MLE_data.povm(:,:,l)'*Rho_out(:,:,j));
%MLE_data.RelFreqOutUncer(l,j) = 0.001;
end
end
MLE_data.method = 'Process';
MLE_data.NumIter = 200;
MLE_data.tol_MLE = 1e-4;
MLE_data.TrPres = 'Yes';
MLE_data.NumMCIter = 1;
% Perform MLE estimation
save('InputData_MLE','MLE_data');
MLE_estimation_iterative_031219;
load('Results_MLE_Process');
% Calculate Fidelities and purities
Fidelity = Chi_MLE(1,1);
Z(a)= Fidelity;
end
save('output.mat','Z')
Stephen23
Stephen23 on 11 Feb 2024
Edited: Stephen23 on 11 Feb 2024
"I am getting error as: Unrecognized field name "X". (Line 61)"
You need to change the fieldnames into the name of your matrix, i.e.:
S_out = S.S_out
This is better than LOADing directly into the workspace.
Thanks for the correction. The next problem is I can see 4 diffrent values of Fidelity in the command window as 0.9952, 0.7044, 0.9979, 0.9971. But the matrix Z is not correct, it is a 6x1 matrix with first 4 entry as NaN, 5th entry as 0 and 6th entry as 0.9971. I have attached a screenshot.
Stephen23
Stephen23 on 11 Feb 2024
Edited: Stephen23 on 11 Feb 2024
"The next problem is I can see 4 diffrent values of Fidelity in the command window as 0.9952, 0.7044, 0.9979, 0.9971. But the matrix Z is not correct, it is a 6x1 matrix with first 4 entry as NaN, 5th entry as 0 and 6th entry as 0.9971. I have attached a screenshot."
Z should have the same number of elements as there are files identified by DIR.
I see from the screenshot that N is 4, which means that your script is most likely modifying Z or a. This is why you should avoid scripts: they make code unpredictable, untestable, unreliable, unrepeatable... and cause bugs.
Like this one.
When I open your script I can see that it uses a for several loop iterators, which would cause that exact bug.
The temporary solution is to rename some of those variables.
The permament solution is to write better code:
Thank you very much for your help. I was able to solve the problem.
@akriti: Please remember to click the accept button if it helped!

Sign in to comment.

More Answers (0)

Categories

Asked:

on 11 Feb 2024

Commented:

on 12 Feb 2024

Community Treasure Hunt

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

Start Hunting!