MATLAB: Filling an empty matrix using a for loop

filling empty matrixMATLAB

for j = abs(1:20:size(Bz,1))
Yi = Bz(j,minK(1,1));
lateral = find_coordinate(R2,Z2,Bz,Yi,0);
M_tot_spions_z = langevinfunc2(n,Kb,T,Brex(j,minK(1,1)), f, Vc, Ms,N);
U_det_r = abs((Yi.*M_tot_spions_z)/100e-9); % Counts 100[nV] Induced voltage [V]
figure;
plot(T_comb,U_det_r)
title([' Detected signal @ lateral distance =', num2str(abs(lateral)),' [m]'])
xlabel('Time [s]')
ylabel('Voltage [V]')
hold on
plot(T_comb, Noise)
end
Hello,
Is there a possibity to save all values for U_det_r so I can use these later on in a different plot?

Best Answer

You can define U_det_r as a cell array or normal array and store the values corresponding to each iteration as follows:
U_det_r = {};
for j = abs(1:20:size(Bz,1))
Yi = Bz(j,minK(1,1));
lateral = find_coordinate(R2,Z2,Bz,Yi,0);
M_tot_spions_z = langevinfunc2(n,Kb,T,Brex(j,minK(1,1)), f, Vc, Ms,N);
U_det_r{j} = abs((Yi.*M_tot_spions_z)/100e-9); % Counts 100[nV] Induced voltage [V]
figure;
plot(T_comb,U_det_r{j})
title([' Detected signal @ lateral distance =', num2str(abs(lateral)),' [m]'])
xlabel('Time [s]')
ylabel('Voltage [V]')
hold on
plot(T_comb, Noise)
end
Refer to cell array & Matrices and Arrays documentation for more information.