I am trying to plot acceleration-time graph by using a txt file but I got error.

5 views (last 30 days)
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
Askic V on 6 Jan 2023
Edited: Askic V 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
Muhammet Emin Sari on 6 Jan 2023
Edited: Muhammet Emin Sari on 6 Jan 2023
Thank you for your comment. It looks easier to understand and it really helped me to solve my problem. <3
But I couldn't understand the problme in my code.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 6 Jan 2023
The columns are reversed from your assignments. The first column is the signal, and the second column is the time vector.
Try something like this —
acc = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1254552/at2.txt');
t = acc(:,2);
a = acc(:,1);
vel = cumtrapz(t,a);
dspl = cumtrapz(t, vel);
figure
subplot(3,1,1)
plot(t, a)
grid
ylabel('Acceleration (cm \cdot s^{-2})')
subplot(3,1,2)
plot(t, vel)
grid
ylabel('Velocity (cm \cdot s^{-1})')
subplot(3,1,3)
plot(t, dspl)
grid
ylabel('Displacement (cm)')
xlabel('Time (s)')
Fs = 1/mean(diff(t));
Fn = Fs/2;
L = numel(t);
NFFT = 2^nextpow2(L);
FTa = fft(a,NFFT)/L;
Fv = linspace(0, 1, NFFT/2+1)*Fn;
Iv = 1:numel(Fv);
figure
plot(Fv, abs(FTa(Iv))*2)
grid
xlabel('Frequency (Hz)')
ylabel('Magnitude Of Acceleration Signal')
I am not certain what the Fourier transform is supposed to be. Make appropriate changes to that part of the code to get the desired result.
.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!