Running a function multiple times and recording outputs for each of the runs in one table.

2 views (last 30 days)
I have a set of data (125,000 rows, 38 columns) and I want to run this code for each column in a matrix starting from column 2. Then, create 2 tables from the outputs. Colum ONE is the time ( a common column) that is used in the calculations.
Here are the codes:
%import xlsx data
dat=importdata('DataBook.xlsx');
t=dat.data(:,1); %time
so=dat.data(:,2); %original signal( this is only for one column, I need the same code to repeat itself for the remaining columns (from 2 to 38)
%frequency space
kHz=1e3;
f_us=27.441*kHz;
T_us=1/f_us;
dt=t(2)-t(1);
Fs=1/dt; %sampling frequency
f=linspace(1,Fs,length(t));
%Fourier Transform
SO=fft(so);
figure(1); plot(f,abs(SO)); xlim([0 Fs/2]); xlabel('Frequency [Hz]');
%theoretical undersampling frequency
ncycle=ceil(dt/T_us); %=9
tau=T_us*ncycle-dt;
T_fake_theory=(T_us/tau)*dt;
F_fake_theory=1/T_fake_theory; %684 Hz
%practical undersampling frequency due to stage move
SOa=abs(SO);
F_fake=f(find(SOa(1:round(length(SOa)/2))==max(SOa))); %663.9 Hz
T_fake=1/F_fake;
%IQ-signal
s_cos=so.*cos(2*pi*F_fake*t);
s_sin=so.*sin(2*pi*F_fake*t);
%move-mean
N_mean_cycle=100;
mean_point=round((N_mean_cycle*T_fake)/dt);
mean_cos = 2*movmean(s_cos,[mean_point mean_point]);
mean_sin = 2*movmean(s_sin,[mean_point mean_point]);
mean_abs=sqrt(mean_cos.^2+mean_sin.^2); %result of A(t) (I need these values in a table for all columns; a matrix of 125000 rows and 37 columns)
mean_phase=atan2(mean_sin,mean_cos); %result of theta(t) (I need these values in a table for all columns; a matrix of 125000 rows and 37 columns)

Accepted Answer

VBBV
VBBV on 14 May 2023
Assuming some random data, the output data can be fit into a table as shown below
% some random data
data= randi([2 200],125000,38); %importdata('DataBook.xlsx');
t=data(:,1); %time
so=data(:,2:38);
%original signal( this is only for one column,
% I need the same code to repeat itself for the remaining columns (from 2 to 38)
%frequency space
kHz=1e3;
f_us=27.441*kHz;
T_us=1/f_us;
dt=t(2)-t(1);
Fs=1/dt; %sampling frequency
f=linspace(1,Fs,length(t));
%Fourier Transform
SO=fft(so);
% figure(1); plot(f,abs(SO)); xlabel('Frequency [Hz]');
%theoretical undersampling frequency
ncycle=ceil(dt/T_us); %=9
tau=T_us*ncycle-dt;
T_fake_theory=(T_us/tau)*dt;
F_fake_theory=1/T_fake_theory; %684 Hz
%practical undersampling frequency due to stage move
SOa=abs(SO);
for k = 1:size(SOa,2)
F_fake(:,k)=f(find(SOa(1:round(length(SOa)/2),k)==max((SOa(:,k)))));
end %663.9 Hz
T_fake=1./F_fake;
%IQ-signal
s_cos=so.*cos(2*pi*F_fake.*t);
s_sin=so.*sin(2*pi*F_fake.*t);
%move-mean
N_mean_cycle=100;
% mean_point=round((N_mean_cycle*T_fake)./dt)
% mean_cos = 2*movmean(s_cos,[mean_point mean_point+5]);
% mean_sin = 2*movmean(s_sin,[mean_point mean_point+5]);
% mean_abs=sqrt(mean_cos.^2+mean_sin.^2)
%result of A(t) (I need these values in a table for all columns; a matrix of 125000 rows and 37 columns)
% mean_phase=atan2(mean_sin,mean_cos)
TT = table(s_cos,s_sin,'VariableNames',{'s_cos','s_sin'})
TT = 125000×2 table
s_cos s_sin ___________ ___________ 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double
  5 Comments
VBBV
VBBV on 15 May 2023
Edited: VBBV on 15 May 2023
the mean_point must be a positive scalar value if you want to use movmean function, The old syntax for movmean that was used in your code needs to be used ONLY for a vector NOT for a matrix.
% some random data
data= randi([2 200],125000,38); %importdata('DataBook.xlsx');
t=data(:,1); %time
so=data(:,2:38);
%original signal( this is only for one column,
% I need the same code to repeat itself for the remaining columns (from 2 to 38)
%frequency space
kHz=1e3;
f_us=27.441*kHz;
T_us=1/f_us;
dt=t(2)-t(1);
Fs=1/dt; %sampling frequency
f=linspace(1,Fs,length(t));
%Fourier Transform
SO=fft(so);
% figure(1); plot(f,abs(SO)); xlabel('Frequency [Hz]');
%theoretical undersampling frequency
ncycle=ceil(dt/T_us); %=9
tau=T_us*ncycle-dt;
T_fake_theory=(T_us/tau)*dt;
F_fake_theory=1/T_fake_theory; %684 Hz
%practical undersampling frequency due to stage move
SOa=abs(SO);
for k = 1:size(SOa,2)
F_fake(:,k)=f(find(SOa(1:round(length(SOa)/2),k)==max((SOa(:,k)))));
end %663.9 Hz
T_fake=1./F_fake;
%IQ-signal
s_cos=so.*cos(2*pi*F_fake.*t)
s_cos = 125000×37
84 8 109 172 163 144 16 43 153 112 125 60 4 172 79 132 136 162 31 193 150 13 140 182 91 134 32 119 103 7 158 138 136 80 50 12 133 34 31 170 189 45 178 111 70 153 14 104 140 46 22 80 6 44 161 40 15 90 36 178 34 5 183 140 185 57 140 28 62 94 7 128 45 184 108 84 23 76 107 153 138 81 2 130 69 95 151 146 24 116 129 131 45 84 77 141 110 60 122 49 92 17 71 82 162 13 177 144 89 38 16 164 120 182 56 97 170 174 118 44 177 12 133 139 115 85 62 127 18 108 43 106 24 80 200 181 182 98 15 57 145 162 84 82 180 156 60 82 194 21 149 194 194 57 43 116 91 25 102 194 103 158 164 44 81 137 92 85 58 104 72 102 169 108 116 25 34 164 104 9 122 82 186 65 148 66 43 76 16 128 120 10 45 185 60 30 148 62 113 126 52 95 28 133 112 7 2 131 135 180 16 108 53 162 73 192 25 124 23 106 85 161 16 102 177 44 128 50 177 64 91 29 5 39 97 183 10 182 183 41 163 45 107 179 24 56 66 136 117 134 130 9 57 31 174 36 116 59 48 175 135 13 157 106 76 154 93 59 170 177 47 192 114 191 137 126 37 35 113 161 134 57 11 139 181 200 133 121 176 126 115 132 57 166 64 117 191 139 190 127
s_sin=so.*sin(2*pi*F_fake.*t)
s_sin = 125000×37
1.0e-10 * 0.0033 0.0003 0.0043 0.0067 0.0064 0.0057 0.0006 0.0017 0.0060 0.0044 0.0049 0.0024 0.0002 0.0067 0.0031 0.0052 0.0053 0.0064 0.0012 0.0076 0.0059 0.0005 0.0055 0.0071 0.0036 0.0053 0.0013 0.0047 0.0040 0.0003 -0.1455 -0.1271 -0.1253 -0.0737 -0.0461 -0.0111 -0.1225 -0.0313 -0.0286 -0.1566 -0.1741 -0.0414 -0.1640 -0.1022 -0.0645 -0.1409 -0.0129 -0.0958 -0.1290 -0.0424 -0.0203 -0.0737 -0.0055 -0.0405 -0.1483 -0.0368 -0.0138 -0.0829 -0.0332 -0.1640 -0.0113 -0.0017 -0.0610 -0.0466 -0.0616 -0.0190 -0.0466 -0.0093 -0.0207 -0.0313 -0.0023 -0.0426 -0.0150 -0.0613 -0.0360 -0.0280 -0.0077 -0.0253 -0.0357 -0.0510 -0.0460 -0.0270 -0.0007 -0.0433 -0.0230 -0.0317 -0.0503 -0.0486 -0.0080 -0.0386 -0.0506 -0.0513 -0.0176 -0.0329 -0.0302 -0.0553 -0.0431 -0.0235 -0.0478 -0.0192 -0.0361 -0.0067 -0.0278 -0.0321 -0.0635 -0.0051 -0.0694 -0.0564 -0.0349 -0.0149 -0.0063 -0.0643 -0.0470 -0.0713 -0.0219 -0.0380 -0.0666 -0.0682 -0.0463 -0.0172 0.0312 0.0021 0.0235 0.0245 0.0203 0.0150 0.0109 0.0224 0.0032 0.0191 0.0076 0.0187 0.0042 0.0141 0.0353 0.0320 0.0321 0.0173 0.0026 0.0101 0.0256 0.0286 0.0148 0.0145 0.0318 0.0275 0.0106 0.0145 0.0342 0.0037 -0.0350 -0.0456 -0.0456 -0.0134 -0.0101 -0.0273 -0.0214 -0.0059 -0.0240 -0.0456 -0.0242 -0.0372 -0.0386 -0.0103 -0.0190 -0.0322 -0.0216 -0.0200 -0.0136 -0.0245 -0.0169 -0.0240 -0.0397 -0.0254 -0.0273 -0.0059 -0.0080 -0.0386 -0.0245 -0.0021 -0.0454 -0.0305 -0.0693 -0.0242 -0.0551 -0.0246 -0.0160 -0.0283 -0.0060 -0.0477 -0.0447 -0.0037 -0.0168 -0.0689 -0.0223 -0.0112 -0.0551 -0.0231 -0.0421 -0.0469 -0.0194 -0.0354 -0.0104 -0.0495 -0.0417 -0.0026 -0.0007 -0.0488 -0.0503 -0.0670 -0.0047 -0.0317 -0.0156 -0.0476 -0.0215 -0.0564 -0.0073 -0.0364 -0.0068 -0.0312 -0.0250 -0.0473 -0.0047 -0.0300 -0.0520 -0.0129 -0.0376 -0.0147 -0.0520 -0.0188 -0.0267 -0.0085 -0.0015 -0.0115 -0.0285 -0.0538 -0.0029 -0.0535 -0.0538 -0.0120 -0.0639 -0.0176 -0.0419 -0.0702 -0.0094 -0.0219 -0.0259 -0.0533 -0.0459 -0.0525 -0.0510 -0.0035 -0.0223 -0.0122 -0.0682 -0.0141 -0.0455 -0.0231 -0.0188 -0.0686 -0.0529 -0.0051 -0.0615 -0.0415 -0.0298 -0.0604 -0.0365 -0.0231 -0.0666 -0.0694 -0.0083 -0.0339 -0.0201 -0.0337 -0.0242 -0.0222 -0.0065 -0.0062 -0.0199 -0.0284 -0.0236 -0.0101 -0.0019 -0.0245 -0.0319 -0.0353 -0.0235 -0.0213 -0.0310 -0.0222 -0.0203 -0.0233 -0.0101 -0.0293 -0.0113 -0.0206 -0.0337 -0.0245 -0.0335 -0.0224
% simple mean
mean_abs=sqrt(mean(s_cos.^2)+mean(s_sin.^2))
mean_abs = 1×37
116.1277 116.3332 116.2554 116.3217 116.2471 116.1877 116.3387 115.9653 116.0115 116.2819 116.0802 116.2234 116.2719 116.1010 116.0759 116.0985 116.2323 116.0055 116.0589 116.1698 116.0326 116.3168 116.0867 116.0492 116.0379 116.1144 116.2015 116.2168 116.2495 116.4357
%result of A(t) (I need these values in a table for all columns; a matrix of 125000 rows and 37 columns)
mean_phase=atan2(mean(s_sin),mean(s_cos))
mean_phase = 1×37
1.0e-13 * -0.2619 -0.2628 -0.2616 -0.2628 -0.2620 -0.2630 -0.2626 -0.2632 -0.2621 -0.2628 -0.2616 -0.2616 -0.2628 -0.2618 -0.2611 -0.2618 -0.2627 -0.2612 -0.2618 -0.2628 -0.2616 -0.2612 -0.2616 -0.2619 -0.2617 -0.2613 -0.2609 -0.2626 -0.2626 -0.2618
N_mean_cycle=100;
% the mean point must be a positive value if you want to use movmean
mean_point=round((N_mean_cycle*T_fake)./dt)
mean_point = 1×37
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
% this syntax needs to be used ONLY for a vector NOT for matrix
% mean_cos = 2*movmean(s_cos,[mean_point mean_point+5]);
% mean_sin = 2*movmean(s_sin,[mean_point mean_point+5]);
% this syntax is used for matrix
mean_cos = 2*movmean(s_cos,max(mean_point), 2);
mean_sin = 2*movmean(s_sin,max(mean_point), 2);
mean_abs=sqrt((mean_cos.^2)+(mean_sin.^2))
mean_abs = 125000×37
168 16 218 344 326 288 32 86 306 224 250 120 8 344 158 264 272 324 62 386 300 26 280 364 182 268 64 238 206 14 316 276 272 160 100 24 266 68 62 340 378 90 356 222 140 306 28 208 280 92 44 160 12 88 322 80 30 180 72 356 68 10 366 280 370 114 280 56 124 188 14 256 90 368 216 168 46 152 214 306 276 162 4 260 138 190 302 292 48 232 258 262 90 168 154 282 220 120 244 98 184 34 142 164 324 26 354 288 178 76 32 328 240 364 112 194 340 348 236 88 354 24 266 278 230 170 124 254 36 216 86 212 48 160 400 362 364 196 30 114 290 324 168 164 360 312 120 164 388 42 298 388 388 114 86 232 182 50 204 388 206 316 328 88 162 274 184 170 116 208 144 204 338 216 232 50 68 328 208 18 244 164 372 130 296 132 86 152 32 256 240 20 90 370 120 60 296 124 226 252 104 190 56 266 224 14 4 262 270 360 32 216 106 324 146 384 50 248 46 212 170 322 32 204 354 88 256 100 354 128 182 58 10 78 194 366 20 364 366 82 326 90 214 358 48 112 132 272 234 268 260 18 114 62 348 72 232 118 96 350 270 26 314 212 152 308 186 118 340 354 94 384 228 382 274 252 74 70 226 322 268 114 22 278 362 400 266 242 352 252 230 264 114 332 128 234 382 278 380 254
%result of A(t) (I need these values in a table for all columns; a matrix of 125000 rows and 37 columns)
mean_phase=atan2(mean_sin,mean_cos)
mean_phase = 125000×37
1.0e-12 * 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 0.0039 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0921 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0333 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 0.0177 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0235 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0372 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0294 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0392 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176 -0.0176
T1 = table(t,'VariableNames',{'time'}) % time
T1 = 125000×1 table
time ____ 42 173 49 73 131 67 123 149 102 101 133 190 52 104 6 195
T2 = table(mean_cos,mean_sin,'VariableNames',{'mean_cos','mean_sin'}) % outputs
T2 = 125000×2 table
s_cos s_sin ___________ ___________ 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double 1×37 double
T_table = cat(2,T1,T2)
T_table = 125000×3 table
time s_cos s_sin ____ ___________ ___________ 42 1×37 double 1×37 double 173 1×37 double 1×37 double 49 1×37 double 1×37 double 73 1×37 double 1×37 double 131 1×37 double 1×37 double 67 1×37 double 1×37 double 123 1×37 double 1×37 double 149 1×37 double 1×37 double 102 1×37 double 1×37 double 101 1×37 double 1×37 double 133 1×37 double 1×37 double 190 1×37 double 1×37 double 52 1×37 double 1×37 double 104 1×37 double 1×37 double 6 1×37 double 1×37 double 195 1×37 double 1×37 double
Zahra
Zahra on 1 Jul 2023
I am not getting the same values as when I run the function (codes) for each column.
I think the move mean syntax used for matrix is not accurate.

Sign in to comment.

More Answers (0)

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!