how can I get the following graph from this data, changes are required in the code

1 view (last 30 days)
clc
clear all
close all
% Lab 2 Group G2 Data Analysis
%[files,path] = uigetfile({'.txt'},'MultiSelect', 'on');
%if iscell(files)
% for n = 1:length(files)
% data = importdata(fullfile(path,files{n})); % fullfile including path
% save data
%end
%else
% data = importdata(fullfile(path,files));
% save data
%end
%G2test = importdata('G2_A.txt','G2_C.txt');
%fid= fopen('G2_A.txt');
%C= readmatrix('data', 'NumHeaderLines',8);
C= readmatrix('G2_A.txt', 'NumHeaderLines',8);
dG2test = C(:,3);
for i = 1:length(dG2test)
if dG2test(i)<0
dG2test(i) = dG2test
end
end
%Convert to SI Units
accG2test = C(:,1).*9.81;
forceG2test = C(:,2).*0.00981;
dispG2test = C(:,3)./1000;
time = dispG2test./accG2test;
s = smooth(forceG2test);
%Find the peak and plot
[~, idx] = findpeaks(s, MinPeakHeigh = 6);
%minAdult = islocalmin(s);
%minLocs = find(minAdult ==1);
%for j = 1:11
%for k = 5:16-1
% newdata{j,:} = dataAdult(minLocs(k):minLocs(k+1),:);
% newdataAdult{minLocs(1):(minLocs(k+1)-minLocs(k)),:} = newdata;
%end
%end
mid = floor(idx(1:end-1) + diff(idx)/2);
t1 = 1:mid(1);
s1 = s(t1);
%t2 = 1:mid(6);
%s2 = s(t2);
plot(t1,s1);
hold on
Please Refer the attached .txt file for the data

Accepted Answer

Star Strider
Star Strider on 19 Oct 2021
Try something like this —
T1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/771138/G2_A.txt', 'VariableNamingRule','preserve')
T1 = 3238×4 table
Accelerometer (0.001 g) Force-sensor (grams) Depth (0.01 mm) EVENTS _______________________ ____________________ _______________ ______ 986 120 0 NaN 981 -60 0 NaN 984 -60 0 NaN 986 -70 0 NaN 993 -70 0 NaN 997 -70 0 NaN 1000 -70 0 NaN 1005 -70 0 NaN 1005 -70 0 NaN 1000 -70 0 NaN 994 -60 0 NaN 992 -60 0 NaN 991 -80 0 NaN 992 -80 0 NaN 990 -60 0 NaN 994 -60 0 NaN
%Convert to SI Units
accG2test = T1{:,1}.*9.81;
forceG2test = T1{:,2}.*0.00981;
dispG2test = T1{:,3}./1000;
% time = dispG2test./accG2test;
forceG2test2 = smoothdata(forceG2test, 'sgolay', 100);
[Vlys,vlocs] = findpeaks(-forceG2test2, 'MinPeakProminence',0.5); % Valleys Of Smoothed Data
Vlys = [-forceG2test2(1); Vlys];
vlocs = [1;vlocs];
Ts = 1; % Sampling Interval
t = 1:height(T1);
figure
plot(t, forceG2test2)
hold on
plot(t(vlocs), -Vlys, '+r')
hold off
for k = 1:numel(vlocs)-1
idx{k} = vlocs(k):vlocs(k+1); % Index Range For This Segment
tc{k} = t(idx{k}); % Time Vector
sc{k} = forceG2test(idx{k}); % Signal Segment
end
tcmax = max(cellfun(@numel,tc)); % Maximum Segment Length
ensb = zeros(tcmax,numel(idx)); % Preallocate
for k = 1:numel(idx)
ensb(1:numel(idx{k}),k) = sc{k}; % Create 'Ensemble'
end
figure
plot(1:tcmax, ensb) % Plot Ensemble
grid
xlabel('Index')
ylabel('Amplitude')
I have no idea which signal ‘Deflection’ is. This creates san ensemble based on the starting and ending points of each forceG2test’ pulse.
Experiment to get different results.
.

More Answers (0)

Tags

Products

Community Treasure Hunt

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

Start Hunting!