Load all mat files to Workspace and plot

23 views (last 30 days)
Mike Buba
Mike Buba on 3 Nov 2021
Commented: Mathieu NOE on 16 May 2022
Hi,
I am recording signals from DSP in External mode and I can record a certain number of data (depends on sample time, number of signals, heap size, etc.) before a new mat file is created (increment file after one-shot). So I end up with e.g., Data_0.mat, Data_1.mat, Data_2.mat, etc.
I can load each mat file into Workspace and plot. e.g.:
load('Data_8.mat')
figure
subplot(2,1,1)
plot(ScopeData1.time,ScopeData1.signals.values)
grid on
subplot(2,1,2)
plot(ScopeData2.time,ScopeData2.signals.values).
For the next recoded section I have to load another mat file (e.g., Data_9.mat), run the plot script, etc.
How to load all mat files into Workspace and plot them all in one single graph?
Note that the signal name is the same in each mat file (ScopeData1.signals.values and ScopeData2.signals.values) and time is not continuous, i.e., there is a break in time for MATLAB to save this recoding and start another.

Accepted Answer

Mathieu NOE
Mathieu NOE on 3 Nov 2021
hello
this is one example how to load all mat files and sort them in natural order (what matlab does not do by default)
the results is that all data are being concatenated but there is this time gap between the recordings , I don't know how you deal with that
fileDir = pwd;
fileNames = dir(fullfile(fileDir,'Data*.mat')); % get list of data files in directory
fileNames_sorted = natsortfiles({fileNames.name}); % sort file names into natural order
% (download FEX : https://fr.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort)
M= length (fileNames_sorted);
out_data = [];
time1 = [];
signal1 = [];
time2 = [];
signal2 = [];
for f = 1:M
S = load(fullfile(fileDir, fileNames_sorted{f}))
% contatenation of all individual files data
time1 = [time1; S.ScopeData1.time];
signal1 = [signal1; S.ScopeData1.signals.values];
time2 = [time2; S.ScopeData2.time];
signal2 = [signal2; S.ScopeData2.signals.values];
end
figure
subplot(2,1,1)
plot(time1,signal1,'*-')
grid on
subplot(2,1,2)
plot(time2,signal2,'*-');

More Answers (1)

venkat reddy
venkat reddy on 14 May 2022
plz help me to plot the file
close all;clear all;clc;
Nt0 = [8;8];
Nt = Nt0(1)*Nt0(2);
Nr0 = [8;8];
Nr = Nt0(1)*Nt0(2);
Ny = 32;
Nx = 32;
N_RF = 4;
L = 3;
d = 0.5;
H = zeros(Nr, Nt);
X = 1/sqrt(Nt)*exp(-1i*2*pi*rand(Nt, Nx));
mu_X = max(max(abs(X*X'-Nx/Nt*eye(Nt))));
W = 1/sqrt(Nr)*exp(-1i*2*pi*rand(Ny, Nr));
mu_W = max(max(abs(W'*W-Ny/Nr*eye(Nr))));
for i = 1:1000000
X0 = 1/sqrt(Nt)*exp(-1i*2*pi*rand(Nt, Nx));
mu_X0 = max(max(abs(X0*X0'-Nx/Nt*eye(Nt))));
if (mu_X0 < mu_X)
X = X0;
mu_X = mu_X0;
end
W0 = 1/sqrt(Nr)*exp(-1i*2*pi*rand(Ny, Nr));
mu_W0 = max(max(abs(W0'*W0-Ny/Nr*eye(Nr))));
if (mu_W0 < mu_W)
W = W0;
mu_W = mu_W0;
end
end
SNR_list = -5:2.5:20;
sample_num = 3;
nmse_result = zeros(numel(SNR_list),sample_num);
alpha_true = zeros(numel(SNR_list),sample_num,L);
phi_t_true = zeros(numel(SNR_list),sample_num,L,2);
phi_r_true = zeros(numel(SNR_list),sample_num,L,2);
L_result = zeros(numel(SNR_list),sample_num);
theta_result = zeros(numel(SNR_list),sample_num,4,10);
z_result = zeros(numel(SNR_list),sample_num,10);
for sample_ii = 1:sample_num
H = zeros(Nr, Nt);
alpha = zeros(L,1);
alpha(1:L) = (normrnd(0, 1, L, 1) + 1i*normrnd(0, 1, L, 1)) / sqrt(2);
while (find(abs(alpha)<0.01))
alpha(1:L) = (normrnd(0, 1, L, 1) + 1i*normrnd(0, 1, L, 1)) / sqrt(2);
end
alpha = sort(alpha, 'descend');
phi_t = 2*rand(L,2)-1;%virtual AoD
phi_r = 2*rand(L,2)-1;%virtual AoA
for l = 1:L
at = kron(exp(-1i*2*pi*[0:Nt0(1)-1]'*d*phi_t(l,1)), exp(-1i*2*pi*[0:Nt0(2)-1]'*d*phi_t(l,2)));
ar = kron(exp(-1i*2*pi*[0:Nr0(1)-1]'*d*phi_r(l,1)), exp(-1i*2*pi*[0:Nr0(2)-1]'*d*phi_r(l,2)));
H = H + alpha(l)*ar*at';
end
for snr_ii = 1:numel(SNR_list)
snr = SNR_list(snr_ii);
noise = sqrt(10^(-snr/10)/2);
H = zeros(Nr, Nt);
alpha = zeros(L,1);
alpha(1:L) = (normrnd(0, 1, L, 1) + 1i*normrnd(0, 1, L, 1)) / sqrt(2);
while (find(abs(alpha)<0.01))
alpha(1:L) = (normrnd(0, 1, L, 1) + 1i*normrnd(0, 1, L, 1)) / sqrt(2);
end
alpha = sort(alpha, 'descend');
phi_t = 2*rand(L,2)-1;%virtual AoD
phi_r = 2*rand(L,2)-1;%virtual AoA
for l = 1:L
at = kron(exp(-1i*2*pi*[0:Nt0(1)-1]'*d*phi_t(l,1)), exp(-1i*2*pi*[0:Nt0(2)-1]'*d*phi_t(l,2)));
ar = kron(exp(-1i*2*pi*[0:Nr0(1)-1]'*d*phi_r(l,1)), exp(-1i*2*pi*[0:Nr0(2)-1]'*d*phi_r(l,2)));
H = H + alpha(l)*ar*at';
end
Y = W*(H*X + noise*(normrnd(0, 1, Nr, Nx) + 1i*normrnd(0, 1, Nr, Nx)));
Rth = noise*sqrt(Ny*Nx);
[theta_es,z_es,err]=IR_SURE_CE_UPA(Y,X,W,Nx,Nt0,Nr0,Ny,Rth);
H_es = zeros(Nr, Nt);
at = zeros(Nt,1);
ar = zeros(Nr,1);
for l = 1:numel(z_es)
at = kron(exp(-1i*2*pi*[0:Nt0(1)-1]'*theta_es(1,l)), exp(-1i*2*pi*[0:Nt0(2)-1]'*theta_es(2,l)));
ar = kron(exp(-1i*2*pi*[0:Nt0(1)-1]'*theta_es(3,l)), exp(-1i*2*pi*[0:Nt0(2)-1]'*theta_es(4,l)));
H_es = H_es + z_es(l)*ar*at';
end
nmse_sample = sum(sum(abs(H-H_es).^2))/sum(sum(abs(H).^2));
disp(['sample_ii=' num2str(sample_ii) ' snr=' num2str(SNR_list(snr_ii)) ' nmse=' num2str(nmse_sample) ' err=' num2str(err)]);
%% spectral efficiency
[U_perfectCSI,S,V_perfectCSI] = svd(H);
P_perfectCSI = V_perfectCSI(:,1:N_RF);
Q_perfectCSI = U_perfectCSI(:,1:N_RF);
R_perfectCSI = 0.5*noise*noise*(Q_perfectCSI'*Q_perfectCSI);
SE_perfectCSI = log2(det(eye(N_RF)+(1/N_RF)*inv(R_perfectCSI)*((Q_perfectCSI'*H*P_perfectCSI)*(Q_perfectCSI'*H*P_perfectCSI)')));
[U_estCSI_SURE,S,V_estCSI_SURE] = svd(H_es);
P_estCSI_SURE = V_estCSI_SURE(:,1:N_RF);
Q_estCSI_SURE = U_estCSI_SURE(:,1:N_RF);
R_estCSI_SURE = 0.5*noise*noise*(Q_estCSI_SURE'*Q_estCSI_SURE);
SE_estCSI_SURE = log2(det(eye(N_RF)+(1/N_RF)*inv(R_estCSI_SURE)*((Q_perfectCSI'*H*P_estCSI_SURE)*(Q_perfectCSI'*H*P_estCSI_SURE)')));
alpha_true(snr_ii, sample_ii,:) = alpha;
phi_t_true(snr_ii, sample_ii,:,:) = phi_t;
phi_r_true(snr_ii, sample_ii,:,:) = phi_r;
nmse_result(snr_ii, sample_ii) = nmse_sample;
L_sample = numel(z_es);
L_result(snr_ii, sample_ii) = L_sample;
theta_result(snr_ii, sample_ii, :, 1:L_sample) = theta_es(:,:);
z_result(snr_ii, sample_ii, 1:L_sample) = z_es(:);
end
save result.mat
load result.mat
figure;
hold on; box on;
plot(snr,result,'rd-','LineWidth',1);
% plot(nmse_result,SE_estCSI_SURE,'ko:','LineWidth',1);
% plot(nmse_result,V_estCSI_SURE,'k*--','LineWidth',1);
% plot(nmse_result,V_perfectCSI,'bs-','LineWidth',1);
%
xlabel('SNR (dB)');
ylabel('NMSE');
legend('ULA','UPA','OMP','AUXILARY BEAM PAIR','Location','NorthWest');
set(gca,'XScale','log');
title('NMSE performance comparison of different channel estimation schemes under NLoS channel');
end
  2 Comments
Mathieu NOE
Mathieu NOE on 16 May 2022
maybe I fixed the previous error - could find the rndcheck function on internet
but now Ihave another issue as this function is not provided in your code :
Unrecognized function or variable 'IR_SURE_CE_UPA'.
Error in venkat (line 80)
[theta_es,z_es,err]=IR_SURE_CE_UPA(Y,X,W,Nx,Nt0,Nr0,Ny,Rth);

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!