Why are my variables saving only Nan entries?
Mostra commenti meno recenti
Cal = struct;
Cal.data = readtable('Lab_4_Data.txt');
Cal.data_uE1 = Cal.data.Var3;
Cal.data_uE2 = Cal.data.Var4;
Cal.data_uE3 = Cal.data.Var5;
Cal.data_E1 = Cal.data_uE1 * 1000000;
Cal.data_E2 = Cal.data_uE2 * 1000000;
Cal.data_E3 = Cal.data_uE3 * 1000000;
masses = [0 50 100 200 300 500]; % grams
xN = (masses ./ 1000) * 9.81; % in newtons
% Calculate shear strain (𝛾xy)
Cal.data_gamma_xy = 2 * Cal.data_E2 - (Cal.data_E1 + Cal.data_E3);
% Calculate principal strains (Ɛ1, Ɛ2, Ɛ3)
Cal.data_epsilon_1 = Cal.data_E1;
Cal.data_epsilon_2 = 0.5 * Cal.data_gamma_xy;
Cal.data_epsilon_3 = Cal.data_E3;
% Calculate Poisson's ratio (v)
Cal.data_nu = abs(Cal.data_epsilon_2) ./ Cal.data_epsilon_1;
% Calculate angle of rotation (𝜃𝜃p)
Cal.data_theta_p = atand(Cal.data_gamma_xy ./ (Cal.data_epsilon_1 - Cal.data_epsilon_3));
% Calculate principal stresses (Txx, Tyy)
E = 69000; % Young's Modulus for aluminum in MPa
Cal.data_Txx = ((E) / (1 - Cal.data_nu.^2)) .* (Cal.data_epsilon_1 + Cal.data_nu .* Cal.data_epsilon_2);
Cal.data_Tyy = ((E) / (1 - Cal.data_nu.^2)) .* (Cal.data_epsilon_2 + Cal.data_nu .* Cal.data_epsilon_1);
% Calculate longitudinal stress from the beam flexure equation
P = xN; % applied load in Newtons
L = 0.241; % length of the beam in meters (replace with your actual value)
b = 0.025; % width of the beam in meters (replace with your actual value)
h = 0.00379; % thickness of the beam in meters (replace with your actual value)
c = h / 2;
% distance between gage's mounting line and dent at the end of the beam in meters
L_adjusted = 0.051;
I = (b * h^3) / 12;
Cal.data_sigma_L = (6 * P * L_adjusted) / (b * h^2);
% Calculate load cell calibration factor
Calibration_Factor = max(abs(Cal.data_epsilon_1)) / max(P);
% Plot 1: Principal Strains vs. Applied Load
figure;
plot(xN, Cal.data_epsilon_1(1:6), '-o', 'DisplayName', 'Ɛ1');
hold on;
plot(xN, Cal.data_epsilon_2(1:6), '-o', 'DisplayName', 'Ɛ2');
plot(xN, Cal.data_epsilon_3(1:6), '-o', 'DisplayName', 'Ɛ3');
xlabel('Applied Load (N)');
ylabel('Principal Strains (\mu\epsilon)');
title('Principal Strains vs. Applied Load');
legend('Location', 'Best');
grid on;
% Plot 2: Principal Stresses vs. Applied Load
figure;
plot(xN, Cal.data_Txx(1:6), '-o', 'DisplayName', 'Txx');
hold on;
plot(xN, Cal.data_Tyy(1:6), '-o', 'DisplayName', 'Tyy');
xlabel('Applied Load (N)');
ylabel('Principal Stresses (MPa)');
title('Principal Stresses vs. Applied Load');
legend('Location', 'Best');
grid on;
% Plot 3: Comparison of Measured and Theoretical Longitudinal Stress
figure;
plot(xN, Cal.data_sigma_L(1:6), '-o', 'DisplayName', 'Measured Longitudinal Stress');
hold on;
plot(xN, Cal.data_Txx(1:6), '-o', 'DisplayName', 'Theoretical Longitudinal Stress');
xlabel('Applied Load (N)');
ylabel('Stress (MPa)');
title('Comparison of Measured and Theoretical Longitudinal Stress');
legend('Location', 'Best');
grid on;
% Display Load Cell Calibration Factor
disp('Load Cell Calibration Factor (N/Ɛ):');
disp(Calibration_Factor);
The .txt file is attached.
This code runs, however the variables data_nu, data_theta_p, data_Txx, and data_Tyy within the Cal struct all have Nan values. All other created variables contain the correct data pulled from the .txt file. Why is this happening?
2 Commenti
"Why is this happening?"
Because the first eighty-five rows of channel data are zeros. What do you expect to get when you divide zero by zero?
Date Time 200245-Ch 1 ue 200245-Ch 2 ue 200245-Ch 3 ue
11/11/21 04:09:57:5492 0 0 0
11/11/21 04:09:57:6792 0 0 0
11/11/21 04:09:57:7992 0 0 0
11/11/21 04:09:57:9293 0 0 0
11/11/21 04:09:58:0593 0 0 0
11/11/21 04:09:58:1894 0 0 0
11/11/21 04:09:58:3194 0 0 0
11/11/21 04:09:58:4494 0 0 0
11/11/21 04:09:58:5795 0 0 0
11/11/21 04:09:58:7095 0 0 0
11/11/21 04:09:58:8395 0 0 0
11/11/21 04:09:58:9594 0 0 0
11/11/21 04:09:59:0896 0 0 0
11/11/21 04:09:59:2197 0 0 0
11/11/21 04:09:59:3596 0 0 0
11/11/21 04:09:59:4897 0 0 0
11/11/21 04:09:59:6197 0 0 0
11/11/21 04:09:59:7498 0 0 0
11/11/21 04:09:59:8799 0 0 0
... etc
Risposte (1)
Cal.data_Txx = ((E) ./ (1 - Cal.data_nu.^2)) .* (Cal.data_epsilon_1 + Cal.data_nu .* Cal.data_epsilon_2);
Cal.data_Tyy = ((E) ./ (1 - Cal.data_nu.^2)) .* (Cal.data_epsilon_2 + Cal.data_nu .* Cal.data_epsilon_1);
% ^^ you used the wrong operator here
The first 85 NaN are expected, as I explained in my comment.
7 Commenti
hyu34gyd5
il 9 Dic 2023
"Changing the operator made no difference in my figures."
Because in all of your figures you only plot the first six elements of those vectors, like this:
Cal.data_epsilon_1(1:6)
Once again: the first 85 elements will be NaN because your data are zeros. You are plotting the first 6 NaN elements. Your channel data appear to correspond to be sampled at particular times (total 212 samples), but you are trying to plot them as if they correspond to a vector of six masses. How do those masses correspond to your sampled data?
As far as I can guess, your channel data has six distinct groups, does each group correspond to one mass? In that case you will need map each group to each mass, e.g. using known limits to the channel values, KMEANS, or similar.
Cal = struct;
Cal.data = readtable('Lab_4_Data.txt');
Cal.data_uE1 = Cal.data.Var3;
Cal.data_uE2 = Cal.data.Var4;
Cal.data_uE3 = Cal.data.Var5;
Cal.data_E1 = Cal.data_uE1 * 1000000;
Cal.data_E2 = Cal.data_uE2 * 1000000;
Cal.data_E3 = Cal.data_uE3 * 1000000;
masses = [0 50 100 200 300 500]; % grams
xN = (masses ./ 1000) * 9.81; % in newtons
% Calculate shear strain (𝛾xy)
Cal.data_gamma_xy = 2 * Cal.data_E2 - (Cal.data_E1 + Cal.data_E3);
% Calculate principal strains (Ɛ1, Ɛ2, Ɛ3)
Cal.data_epsilon_1 = Cal.data_E1;
Cal.data_epsilon_2 = 0.5 * Cal.data_gamma_xy;
Cal.data_epsilon_3 = Cal.data_E3;
% Calculate Poisson's ratio (v)
Cal.data_nu = abs(Cal.data_epsilon_2) ./ Cal.data_epsilon_1;
% Calculate angle of rotation (𝜃𝜃p)
Cal.data_theta_p = atand(Cal.data_gamma_xy ./ (Cal.data_epsilon_1 - Cal.data_epsilon_3));
% Calculate principal stresses (Txx, Tyy)
E = 69000; % Young's Modulus for aluminum in MPa
Cal.data_Txx = ((E) ./ (1 - Cal.data_nu.^2)) .* (Cal.data_epsilon_1 + Cal.data_nu .* Cal.data_epsilon_2);
Cal.data_Tyy = ((E) ./ (1 - Cal.data_nu.^2)) .* (Cal.data_epsilon_2 + Cal.data_nu .* Cal.data_epsilon_1);
% Calculate longitudinal stress from the beam flexure equation
P = xN; % applied load in Newtons
L = 0.241; % length of the beam in meters (replace with your actual value)
b = 0.025; % width of the beam in meters (replace with your actual value)
h = 0.00379; % thickness of the beam in meters (replace with your actual value)
c = h / 2;
% distance between gage's mounting line and dent at the end of the beam in meters
L_adjusted = 0.051;
I = (b * h^3) / 12;
Cal.data_sigma_L = (6 * P * L_adjusted) / (b * h^2);
% Calculate load cell calibration factor
Calibration_Factor = max(abs(Cal.data_epsilon_1)) / max(P);
% Plot 1: Principal Strains vs. Applied Load
figure;
plot(Cal.data_epsilon_1, '-o', 'DisplayName', 'Ɛ1');
hold on;
plot(Cal.data_epsilon_2, '-o', 'DisplayName', 'Ɛ2');
plot(Cal.data_epsilon_3, '-o', 'DisplayName', 'Ɛ3');
xlabel('Applied Load (N)');
ylabel('Principal Strains (\mu\epsilon)');
title('Principal Strains vs. Applied Load');
legend('Location', 'Best');
grid on;
% Plot 2: Principal Stresses vs. Applied Load
figure;
plot(Cal.data_Txx, '-o', 'DisplayName', 'Txx');
hold on;
plot(Cal.data_Tyy, '-o', 'DisplayName', 'Tyy');
xlabel('Applied Load (N)');
ylabel('Principal Stresses (MPa)');
title('Principal Stresses vs. Applied Load');
legend('Location', 'Best');
grid on;
% Plot 3: Comparison of Measured and Theoretical Longitudinal Stress
figure;
plot(Cal.data_sigma_L, '-o', 'DisplayName', 'Measured Longitudinal Stress');
hold on;
plot(Cal.data_Txx, '-o', 'DisplayName', 'Theoretical Longitudinal Stress');
xlabel('Applied Load (N)');
ylabel('Stress (MPa)');
title('Comparison of Measured and Theoretical Longitudinal Stress');
legend('Location', 'Best');
grid on;
% Display Load Cell Calibration Factor
disp('Load Cell Calibration Factor (N/Ɛ):');
disp(Calibration_Factor);
"I only want to plot one data point for each of the applied load settings."
But your data file has multiple data points for each group. You need to decide how to obtain one data point from each group: take the mean, the max, the min, the mode, or some other value. Which do you want?
Another important question: how do you distinguish between the groups in the data file? Is there a particular value or parameter that specifies each group? Or a duration that each group was sampled for? Or do we need to identify the groups by a change in the data values?
"I'm not familar with the methods you suggested using."
We can look at the that once we know how you want to calculate one value from each group.
hyu34gyd5
il 10 Dic 2023
Sam Chak
il 10 Dic 2023
Hi @hyu34gyd5
Since the code is provided, most of the time, we can only advise on what went wrong in the code. If you are very new to MATLAB and coding, you may have difficulty understanding some technical aspects that we explain.
Therefore, I suggest that you provide a very clear picture (graphically) of what you intend to do with the data. Also, provide a sample outcome or plot that you expected.
Furthermore, some of us are not experts in the stress and strain field. Thus, we cannot verify if the formulas in the code are correct or not. You are advised to attach some images of the formulas so that we can check them.
You could use K-means clustering to detect the groups:
after which it is very simple to take the mean of each group. First import the data from file:
fnm = 'Lab_4_Data.txt';
opt = detectImportOptions(fnm, 'Delimiter','\t', 'VariableNamingRule','preserve');
tbl = readtable(fnm,opt)
Then identify each group using K-means algorithm and take the mean of each channel in each group:
masses = [0,50,100,200,300,500]; % grams
mat = tbl{:,digitsPattern+wildcardPattern};
idx = kmeans(mat,numel(masses));
[~,~,idy] = unique(idx,'stable');
tmp = splitapply(@mean,mat,idy)
Cal = struct;
Cal.data_uE1 = tmp(:,1);
Cal.data_uE2 = tmp(:,2);
Cal.data_uE3 = tmp(:,3);
After that comes the rest of your code:
Cal.data_E1 = Cal.data_uE1 * 1000000;
Cal.data_E2 = Cal.data_uE2 * 1000000;
Cal.data_E3 = Cal.data_uE3 * 1000000;
xN = (masses ./ 1000) * 9.81; % in newtons
% Calculate shear strain (𝛾xy)
Cal.data_gamma_xy = 2 * Cal.data_E2 - (Cal.data_E1 + Cal.data_E3);
% Calculate principal strains (Ɛ1, Ɛ2, Ɛ3)
Cal.data_epsilon_1 = Cal.data_E1;
Cal.data_epsilon_2 = 0.5 * Cal.data_gamma_xy;
Cal.data_epsilon_3 = Cal.data_E3;
% Calculate Poisson's ratio (v)
Cal.data_nu = abs(Cal.data_epsilon_2) ./ Cal.data_epsilon_1;
% Calculate angle of rotation (𝜃𝜃p)
Cal.data_theta_p = atand(Cal.data_gamma_xy ./ (Cal.data_epsilon_1 - Cal.data_epsilon_3));
% Calculate principal stresses (Txx, Tyy)
E = 69000; % Young's Modulus for aluminum in MPa
Cal.data_Txx = ((E) ./ (1 - Cal.data_nu.^2)) .* (Cal.data_epsilon_1 + Cal.data_nu .* Cal.data_epsilon_2);
Cal.data_Tyy = ((E) ./ (1 - Cal.data_nu.^2)) .* (Cal.data_epsilon_2 + Cal.data_nu .* Cal.data_epsilon_1);
% Calculate longitudinal stress from the beam flexure equation
P = xN; % applied load in Newtons
L = 0.241; % length of the beam in meters (replace with your actual value)
b = 0.025; % width of the beam in meters (replace with your actual value)
h = 0.00379; % thickness of the beam in meters (replace with your actual value)
c = h / 2;
% distance between gage's mounting line and dent at the end of the beam in meters
L_adjusted = 0.051;
I = (b * h^3) / 12;
Cal.data_sigma_L = (6 * P * L_adjusted) / (b * h^2);
% Calculate load cell calibration factor
Calibration_Factor = max(abs(Cal.data_epsilon_1)) / max(P);
% Plot 1: Principal Strains vs. Applied Load
figure;
plot(xN,Cal.data_epsilon_1, '-o', 'DisplayName', 'Ɛ1');
hold on;
plot(xN,Cal.data_epsilon_2, '-o', 'DisplayName', 'Ɛ2');
plot(xN,Cal.data_epsilon_3, '-o', 'DisplayName', 'Ɛ3');
xlabel('Applied Load (N)');
ylabel('Principal Strains (\mu\epsilon)');
title('Principal Strains vs. Applied Load');
legend('Location', 'Best');
grid on;
% Plot 2: Principal Stresses vs. Applied Load
figure;
plot(xN,Cal.data_Txx, '-o', 'DisplayName', 'Txx');
hold on;
plot(xN,Cal.data_Tyy, '-o', 'DisplayName', 'Tyy');
xlabel('Applied Load (N)');
ylabel('Principal Stresses (MPa)');
title('Principal Stresses vs. Applied Load');
legend('Location', 'Best');
grid on;
% Plot 3: Comparison of Measured and Theoretical Longitudinal Stress
figure;
plot(xN,Cal.data_sigma_L, '-o', 'DisplayName', 'Measured Longitudinal Stress');
hold on;
plot(xN,Cal.data_Txx, '-o', 'DisplayName', 'Theoretical Longitudinal Stress');
xlabel('Applied Load (N)');
ylabel('Stress (MPa)');
title('Comparison of Measured and Theoretical Longitudinal Stress');
legend('Location', 'Best');
grid on;
% Display Load Cell Calibration Factor
disp('Load Cell Calibration Factor (N/Ɛ):');
disp(Calibration_Factor);
Categorie
Scopri di più su Stress and Strain in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!








