I am trying to plot acceleration-time graph by using a txt file but I got error.
Show older comments
clear all;close all;clc;
%%%%%%%LOAD ACCELERATION%%%%%%%
[filename]=textscan('at2.txt','%s');
for i=1:1:length(filename)
file=fopen(filename{i});
line1=fgetl(file);
line2=fgetl(file);
line3=fgetl(file);
line4=fgetl(file);
line5=fgetl(file);
line6=fgetl(file);
line7=fgetl(file);
acc=fscanf(file,'%e',[2,inf]);
fclose(file);
acc=acc';
t=acc(:,1);
a=acc(:,2);
dt=0.01;
%%%%%%%%VELOCITY%%%%%%%%
vel=dt*cumtrapz(a);
%%%%%%DISPACEMENT%%%%%%%%
disp=dt*cumtrapz(vel);
figure
plot(t,a,'linewidth',2);title(['Acceleration Time History for Point',num2str(i)]);
ylabel('Acceleration, cm/sec^2');xlabel('Time, sec');grid on;
saveas(gcf,['acc_p',num2str(i)],'jpg')
figure
plot(t,vel,'linewidth',2);title(['Velocity Time History for Point',num2str(i)]);
ylabel('Velocity, cm/sec');xlabel('Time, sec');grid on;
saveas(gcf,['vel_p',num2str(i)],'jpg')
figure
plot(t,disp,'linewidth',2);title(['Displacement Time History for Point',num2str(i)]);
ylabel('Displacement, cm');xlabel('Time, sec');grid on;
saveas(gcf,['dis_p',num2str(i)],'jpg')
figure
subplot(311)
plot(t,a,'linewidth',2);title(['TIME HISTORIES FOR POINT',num2str(i)]);
ylabel('Acceleration, cm/sec^2');grid on;
subplot(312)
plot(t,vel,'linewidth',2);
ylabel('Velocity, cm/sec') ;grid on;
subplot(313)
plot(t,disp,'linewidth',2);grid on;
ylabel('Displacement, cm');xlabel('Time, sec')
saveas(gcf,['TH_p',num2str(i)],'jpg')
% %%%%%%%%%FOURIER SPECTRUM%%%%%%%%%%%
% [FA,f]=fr_amp(a,dt);
%
% figure
% loglog(f,FA,'linewidth',2);grid on;
% xlabel('Frequency, Hz'); ylabel('Fourier Amplitude, cm/sec');
% title(['FOURIER AMPLITUDE SPECTRA of POINT',num2str(i)]);
% saveas(gcf,['FAS_p',num2str(i)],'jpg')
end
4 Comments
Askic V
on 6 Jan 2023
Please attach your file: 'at2.txt', this way you increase your chances to get quick help.
Muhammet Emin Sari
on 6 Jan 2023
First of all, this is more elgant way to read the file:
filename = 'at2.txt';
A = readmatrix(filename);
acc = A(:,1); % first column represents acceleration
time = A(:,2); % second coolumn represent time
Form teh data in the file, it seems that second column is time, because the difference is constant and 0.005, so sample time is 5 ms?
Do you need something like this?
filename = 'at2.txt';
A = readmatrix(filename);
acc = A(:,1); % first column represents acceleration
time = A(:,2); % second coolumn represent time
% time difference is equal to time(2)-time(1)
t_diff = diff(time);
dt = sum(t_diff)/numel(t_diff);
% dt = time(2)-time(1); % same result as above
% plot
subplot(3,1,1)
plot(time, acc)
subplot(3,1,2)
vel = dt * cumtrapz(acc);
plot(time, vel);
subplot(3,1,3)
disp = dt * cumtrapz(vel);
plot(time, disp);
Muhammet Emin Sari
on 6 Jan 2023
Edited: Muhammet Emin Sari
on 6 Jan 2023
Accepted Answer
More Answers (0)
Categories
Find more on Line Plots 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!
