Loading multiple mat files and plot them right

24 views (last 30 days)
I want to plot 3 different .mat files. My Structure looks something like this.
d = dir('*.mat');
Anzmat = length(d);
for j = 1:3 %just use the first 3 Files in the Order structure ->test 1, test 2, test 3
load(d(j).name)
%%%%%%%Calculations%%%%%%%
figure();
plot(t_LP, x_ac)
title('acceleration shorten file x-axis')
xlabel('time (s)')
ylabel('a (m/s^2)')
%%%%%%%Calculations%%%%%%%
figure();
plot(t_LP, y_ac)
title('acceleration shorten file y-axis')
xlabel('time (s)')
ylabel('a (m/s^2)')
%%%%%%%Calculations%%%%%%%
figure();
plot(t_LP, velocity_x)
title('velocity shorten file x-axis')
xlabel('time (s)')
ylabel('v (m/s)')
%%%%%%%Calculations%%%%%%%
figure();
plot(t_LP, velocity_y)
title('velocity shorten file y-axis')
xlabel('time (s)')
ylabel('v (m/s)')
%%%%%%%Calculations%%%%%%%
plot(t_LP,ac,'r')
title('final velocity')
xlabel('time (s)')
ylabel('v (m/s)')
end
Now my aim is to plot the Files: test 1, test 2, test 3 together in one diagramm. So in the end i should have 5 plots and in each are 3 diagramms from the test1,test2,test3 mat data. So i can clearly analyse how different the measurements are in one plot.
  2 Comments
Daniel Basow
Daniel Basow on 18 Jul 2021
Edited: Daniel Basow on 18 Jul 2021
I dont know if its a good solution but my idea is to make following.
At the end of the code i define my ac, t_LP with the numbers from the file. In the outside the For Loop i have all my files seperatly defined an can plot them without any problems.
d = dir('*.mat');
Anzmat = length(d);
for j = 1:3 %just use the first 3 Files in the Order structure ->test 1, test 2, test 3
load(d(j).name)
%%%Calculations%%%%
assignin ('base',['x_ac_' num2str(j)], x_ac);
assignin ('base',['y_ac_' num2str(j)], y_ac);
assignin ('base',['t_LP_' num2str(j)], t_LP);
assignin ('base',['velocity_x_' num2str(j)], velocity_x_);
assignin ('base',['velocity_y_' num2str(j)], velocity_y_);
assignin ('base',['ac_' num2str(j)], ac);
end
hold on
plot(t_LP_1,x_ac_1)
plot(t_LP_2,x_ac_2)
plot(t_LP_3,x_ac_3)
hold off
.....
Does it have a better option to do this?

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 19 Jul 2021
hello
based on the first post you can do that : tested with dummy data ( commented lines) of different size ... so it works whatever the size of each individual file , assumed the same variable names are present in all files :
%% dummy data
% clc
% clearvars
%
% N = 10;
% t_LP = (1:N);
% x_ac = 1*ones(size(t_LP));
% y_ac = 0.1*t_LP;
% velocity_x = 1*randn(size(t_LP));
% velocity_y = 1*sin(t_LP);
% ac = x_ac + y_ac;
% save test1.mat t_LP x_ac y_ac velocity_x velocity_y ac
%
% N = 20;
% t_LP = (1:N);
% x_ac = 2*ones(size(t_LP));
% y_ac = 0.2*t_LP;
% velocity_x = 1*randn(size(t_LP));
% velocity_y = 1.5*sin(t_LP);
% ac = x_ac + y_ac;
% save test2.mat t_LP x_ac y_ac velocity_x velocity_y ac
%
%
% N = 30;
% t_LP = (1:N);
% x_ac = 3*ones(size(t_LP));
% y_ac = 0.3*t_LP;
% velocity_x = 1*randn(size(t_LP));
% velocity_y = 2*sin(t_LP);
% ac = x_ac + y_ac;
% save test3.mat t_LP x_ac y_ac velocity_x velocity_y ac
clc
clearvars
d = dir('test*.mat');
Anzmat = length(d);
for j = 1:3 %just use the first 3 Files in the Order structure ->test 1, test 2, test 3
filename = d(j).name;
load(filename)
legend_string{j} = filename(1:length(filename)-4); % filename without the .mat extension
%%%%%%%Calculations%%%%%%%
figure(1);hold on
plot(t_LP, x_ac)
title('acceleration shorten file x-axis')
xlabel('time (s)')
ylabel('a (m/s^2)')
legend(legend_string);
%%%%%%%Calculations%%%%%%%
figure(2);hold on
plot(t_LP, y_ac)
title('acceleration shorten file y-axis')
xlabel('time (s)')
ylabel('a (m/s^2)')
legend(legend_string);
%%%%%%%Calculations%%%%%%%
figure(3);hold on
plot(t_LP, velocity_x)
title('velocity shorten file x-axis')
xlabel('time (s)')
ylabel('v (m/s)')
legend(legend_string);
%%%%%%%Calculations%%%%%%%
figure(4);hold on
plot(t_LP, velocity_y)
title('velocity shorten file y-axis')
xlabel('time (s)')
ylabel('v (m/s)')
legend(legend_string);
%%%%%%%Calculations%%%%%%%
figure(5);hold on
plot(t_LP,ac);
title('final velocity')
xlabel('time (s)')
ylabel('v (m/s)')
legend(legend_string);
end

More Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!