MATLAB: Is the script returning NaN for values of an array ONLY when used in a for loop

MATLABnan

The problem I am having is that I'm trying to process some data and make some graphs out of it, but some of the arrays that I create will return NaN at specific indices, even though I know that there should not be a problem. I know this because I do the calculation outside of the loop (commenting out the loop, and setting the loop variable to the specific value with which I'm having the issue, then running the script again), and I get the expected result. Does it have something to do with the loop? I'm almost certain that it isn't the data, based on the fact that I can do the calculation outside of the loop and have no problems.
filenames = {'shoulder_cal_1', 'shoulder_cal_2', 'shoulder_cal_3', 'shoulder_cal_4', 'shoulder_cal_5', 'shoulder_cal_6'};
% filenames = {'elbow_cal_1', 'elbow_cal_2', 'elbow_cal_3', 'elbow_cal_4', 'elbow_cal_5', 'elbow_cal_6'};
motor_volts = 6; % Set to 6 for shoulder, set to 7 for elbow
motor_torque = 10; % Set to 10 for shoulder, set to 11 for elbow
world_force_x = 8;
world_force_y = 9;
x_offset = 0.0; % from computer, to convert to coordinates with
y_offset = -0.65; % origin at the motor (in meters)
count = 0;
% for j = 1; % For debugging
for j = (1:length(filenames)); % Option that's actually used
count = count + 1;
disp(count);
file = strcat('File name that I know works');
data = dlmread(file);
data_length = size(data,1);
voltages = ones(1,51);
avg_forces = ones(1,51);
avg_actual_torques = ones(1,51);
avg_motor_torques = ones(1,51);
index = 0;
% for k = 0.300; % For debugging NaN
for k = (0.0000:0.1000:5.0000); % All of the possible voltage values (from volts_torque2 program)
index = index + 1;
force_mag_vals = [];
actual_torque_vals = [];
motor_torque_vals = [];
for i = (1:data_length);
if data(i, motor_volts) == k;
x_pos = (data(i, 2)/1000.0) + x_offset; % /1000 to convert mm to m
y_pos = (data(i, 3)/1000.0) + y_offset;
force_mag = sqrt(data(i, world_force_x)^2 + data(i, world_force_y)^2);
force_mag_vals = [force_mag_vals force_mag];
motor_torque_vals = [motor_torque_vals data(i, motor_torque)];
torque_calc = data(i, world_force_y)*x_pos - data(i, world_force_x)*y_pos;
actual_torque_vals = [actual_torque_vals torque_calc];
end
end
% Calculates the average of the desired quantity at a specific voltage level
avg_forces(index) = mean(force_mag_vals);
avg_motor_torques(index) = mean(motor_torque_vals);
avg_actual_torques(index) = mean(actual_torque_vals);
voltages(index) = k;
end
% % Creation of various graphs that I know work
end
I believe that when the script returns NaN in avg_forces, it is because it performs mean(force_mag_vals) but force_mag_vals is empty for some reason. Perhaps there is another reason. Can anyone see anything obvious as to why my script is doing this?

Best Answer

You are testing for floating point equality instead of testing for a value "close enough". Different methods of calculating floating point values can end up with slightly different bottom bits. For example, linspace() uses a different algorithm than : does, in order to adjust for accumulated round-off error.
Because of accumulated round-off error,
k = 0.0000:0.1000:5.0000
is not the same as
k = (0:1:50) / 10;
differing at
0.3, 0.6, 0.7, 1.2, 1.4, 1.7, 1.9, 2.3, 2.4, 2.6, 2.7, 3.1, 3.6
You should be testing for equality to within a tolerance. You might want to use ismembertol()