Table formatting inside a for loop

1 view (last 30 days)
AL
AL on 15 Mar 2023
Edited: 闻龙 on 15 Mar 2023
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 = 4×4 table
Frequency peak amp Phase peak locations ______________ ____________________ __________________ _______________ 19 19.125 0.20233 2.2867 -104.91 -102.27 191 52 6 14.25 0.10132 1.5843 -76.11 112.51 61 39 13.6 25.5 0.095354 0.29818 18.393 -41.026 137 69 25.3 6 0.022303 0.10936 84.422 75.346 254 17
%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_;

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"])
tbl = 4×2 table
1 2 Frequency peak amp Phase peak locations Frequency peak amp Phase peak locations ___________________________________________________ ___________________________________________________ 19 0.20233 -104.91 191 19.125 2.2867 -102.27 52 6 0.10132 -76.11 61 14.25 1.5843 112.51 39 13.6 0.095354 18.393 137 25.5 0.29818 -41.026 69 25.3 0.022303 84.422 254 6 0.10936 75.346 17

More Answers (0)

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!