Table formatting inside a for loop
1 view (last 30 days)
Show older comments
For given code I like to create a table something like this:
curretly table is like this:
Can anyone teach me how to do the formating of the table like this:
I have attached my code and one reference file.
header = 9;
delimiter = '\t';
for i = 1:2
filname = sprintf('H1, 2_I1sv%05d.txt',i);
dat(i) =importdata(filname,delimiter,header);
FRFdata = readmatrix(filname, 'HeaderLines',9);
ind =2:size(FRFdata,1);
f = FRFdata(:,1);
a = FRFdata(:,2);
b = FRFdata(:,3);
amp = hypot(a,b);
pha = atan2d(b,a);
mpp = max(amp(ind))/30;
[Ypk,Xpk,Wpk,Ppk] = findpeaks(amp(ind),'MinPeakProminence',mpp, 'WidthReference','halfheight');
Xpk = Xpk+min(ind)-1;
[pv,mxidx] = maxk(Ppk,4); % to get top 4 peak prominences
Ypkc{i} = Ypk(mxidx); % peak amp
Xpkc{i} = Xpk(mxidx); % peak locations
Wpkc{i} = Wpk(mxidx); % peak widths
Ppkc{i} = Ppk(mxidx); % peak prominences % Save To Cell Array
freq{i} = f(Xpkc{i}); % Frequency
pha_{i} = pha(Xpkc{i}); % Phase
figure(i)
subplot(2,1,1),plot(f,amp,f(Xpk),Ypk,'dr')
grid on
ylabel('Amp')
xlim([0 40])
ylim("auto")
subplot(2,1,2),plot(f,pha, f(Xpk),pha(Xpk),'dr')
grid on
xlim([0 40])
xlabel('Hz')
ylabel('Phase')
sgtitle("File "+string(i))
end
Results = table([freq{:}],[Ypkc{:}],[pha_{:}],[Xpkc{:}], 'VariableNames',{'Frequency','peak amp','Phase','peak locations '})
%Results = table([freq{:}],[Ypkc{:}],[pha_{:}],[Xpkc{:}],[Wpkc{:}],[Ppkc{:}], 'VariableNames',{'Frequency','peak amp','Phase','peak locations ','peak widths ','peak prominences'})
clear a amp b dat delimiter f filname freq FRFdata;
clear header i ind mpp mxidx Ppk Ppkc pv Wpk Wpkc Xpk Xpkc Ypk Ypkc pha pha_;
0 Comments
Accepted Answer
闻龙
on 15 Mar 2023
Edited: 闻龙
on 15 Mar 2023
You can merge two tables into one, like this:
tbl1 = table(freq{1}, Ypkc{1}, pha_{1}, Xpkc{1}, 'VariableNames',{'Frequency','peak amp','Phase','peak locations '});
tbl2 = table(freq{2}, Ypkc{2}, pha_{2}, Xpkc{2}, 'VariableNames',{'Frequency','peak amp','Phase','peak locations '});
tbl = table(tbl1, tbl2, 'VariableNames', ["1", "2"])
0 Comments
More Answers (0)
See Also
Categories
Find more on Data Import and Analysis 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!