for loop executing else statement every time!

2 views (last 30 days)
Hello. Right now, the else branch of the if/then statement is executing every single time so the probability is always 100% for each beetle in one of the behavior categories, regardless of the values placed in the if statement resulting in the graph below.
Based off of the slope of figure 2 which is TT(k)/msd(:,1), I would like to make a bar graph that shows the results if the slope is between 0.75-1.5 (ballistic behavior)-- otherwise, the behavior is diffusive. Here is the error I keep getting:
Index exceeds the number of array elements (5).
Error in MeanSquaredDispCharTime1new (line 58)
behaviorSum(2*i)=behaviorSum(2*i)+1; %incrementing diffusive bin
Here is an excerpt of my code. Thank you so much!
LET = ['ABCDE']; %# letters represent # of beetles; ie ABC means there are 3 beetles
for e = 1:2
%%%% 0 frequency
if e == 1, freq = 0;
% figure(e*10+1); clf; hold on;
% figure(e*10+2); clf; hold on;
figure(e*10+3); clf; hold on;
for j = 1:5
clear data
data = csvread(['Control4' LET(j) '.csv']); %yes
nData = size(data,1); %# number of data points
numberOfDeltaT = floor(nData); %# for MSD, dt should be up to 1/4 of number of data points
msd = zeros(numberOfDeltaT,3); %# We'll store [mean, std, n]
%# calculate msd for all deltaT's
for dt = 1:numberOfDeltaT
deltaCoords = data(1+dt:end,1:2) - data(1:end-dt,1:2);
squaredDisplacement = sum(deltaCoords.^2,2); %# dx^2+dy^2+dz^2
msd(dt,1) = mean(squaredDisplacement); %# average
msd(dt,2) = std(squaredDisplacement); %# std
msd(dt,3) = length(squaredDisplacement); %# n
end
% figure(e*10+1);
% plot(data(:,1), data(:,2)); axis equal;
%
% figure(e*10+2);
TT = (1:numberOfDeltaT);
% h = errorbar(TT, msd(:,1), msd(:,2), 'o', 'markersize', 12);
% h = plot(TT, msd(:,1), 'o', 'markersize', 12);
% h.LineWidth = 2;
% set(gca, 'xscale', 'log', 'yscale', 'log');
% plot(xlim, ylim, '--k', 'markersize', 4) %GOAL: place a line in the middle so we know where slope = 1 as a reference
% xlabel('i\deltat(s)');
% ylabel('\langle\Deltax_{i}^{2}\rangle/\langle\Deltax_{1}^{2}\rangle');
% title('Normalized Mean Square Displacements vs. Time Difference for Control 4')
figure(e*10+3);
behaviors= 2; % 2 behaviors
beetles=5; %ideally will use LET so the loop worked regardless of if 4 or 5 beetles and not a manual input
count= zeros(1,beetles*behaviors); % creates 2 empty 'bins' or storage areas for ballistic or diffusive
yValues=nData*beetles;
% Count outcomes of behavior of beetles
behaviorSum=zeros(1,beetles*behaviors);
for k= 1:nData
for i=1:beetles
if (1.5 >= TT(k)/(msd(k,1))) && (TT(k)/(msd(k,1)) >= 0.75) %ballistic
behaviorSum(2*i-1)=behaviorSum(2*i-1)+1; %incrementing ballistic bin
else
behaviorSum(2*i)=behaviorSum(2*i)+1; %incrementing diffusive bin
end
% increment appropriate bin
end
%this is storing a value in one of the bins created
end
for n=1:beetles*behaviors
count(n)=behaviorSum(n)/nData;
end
end
% Show bar graph of outcome
bar(1:beetles*behaviors, count) %this is creating the histogram with 1:behaviors as number of bars and count as y values for bars
title(sprintf('Characteristic time \deltat_{c} vs. Probability', yValues), 'Fontsize',14)
xlabel('Characteristic time \deltat_{c}', 'Fontsize',14)
ylabel('Probability', 'Fontsize',14)
shg %show graph window
else
fprintf('Thanks for helping!');
end
end
Attached below is an example csv file:
  1 Comment
Mathieu NOE
Mathieu NOE on 15 Apr 2022
hello
with the supplied csv file there is no error poping up , at least on my R2020b
also there is no figure 2 showing up, so I ended up ploting myself the TT(k)/(msd(k,1)) array , which, for the csv file provided, is above 5 ; the if statement (ballistic) is never met
if (1.5 >= TT(k)/(msd(k,1))) && (TT(k)/(msd(k,1)) >= 0.75) %ballistic
- is that your issue ?
clc
clearvars
% LET = ['ABCDE']; %# letters represent # of beetles; ie ABC means there are 3 beetles
LET = ['A']; %# letters represent # of beetles; ie ABC means there are 3 beetles
for e = 1:2
%%%% 0 frequency
if e == 1, freq = 0;
% figure(e*10+1); clf; hold on;
% figure(e*10+2); clf; hold on;
figure(e*10+3); clf; hold on;
for j = 1:1%5
clear data
data = csvread(['Control4' LET(j) '.csv']); %yes
nData = size(data,1); %# number of data points
numberOfDeltaT = floor(nData); %# for MSD, dt should be up to 1/4 of number of data points
msd = zeros(numberOfDeltaT,3); %# We'll store [mean, std, n]
%# calculate msd for all deltaT's
for dt = 1:numberOfDeltaT
deltaCoords = data(1+dt:end,1:2) - data(1:end-dt,1:2);
squaredDisplacement = sum(deltaCoords.^2,2); %# dx^2+dy^2+dz^2
msd(dt,1) = mean(squaredDisplacement); %# average
msd(dt,2) = std(squaredDisplacement); %# std
msd(dt,3) = length(squaredDisplacement); %# n
end
% figure(e*10+1);
% plot(data(:,1), data(:,2)); axis equal;
%
% figure(e*10+2);
TT = (1:numberOfDeltaT);
% h = errorbar(TT, msd(:,1), msd(:,2), 'o', 'markersize', 12);
% h = plot(TT, msd(:,1), 'o', 'markersize', 12);
% h.LineWidth = 2;
% set(gca, 'xscale', 'log', 'yscale', 'log');
% plot(xlim, ylim, '--k', 'markersize', 4) %GOAL: place a line in the middle so we know where slope = 1 as a reference
% xlabel('i\deltat(s)');
% ylabel('\langle\Deltax_{i}^{2}\rangle/\langle\Deltax_{1}^{2}\rangle');
% title('Normalized Mean Square Displacements vs. Time Difference for Control 4')
figure(e*10+3);
behaviors= 2; % 2 behaviors
beetles=5; %ideally will use LET so the loop worked regardless of if 4 or 5 beetles and not a manual input
count= zeros(1,beetles*behaviors); % creates 2 empty 'bins' or storage areas for ballistic or diffusive
yValues=nData*beetles;
% Count outcomes of behavior of beetles
behaviorSum=zeros(1,beetles*behaviors);
for k= 1:nData
for i=1:beetles
my_test_data(k) = TT(k)/(msd(k,1));
if (1.5 >= TT(k)/(msd(k,1))) && (TT(k)/(msd(k,1)) >= 0.75) %ballistic
behaviorSum(2*i-1)=behaviorSum(2*i-1)+1; %incrementing ballistic bin
else
behaviorSum(2*i)=behaviorSum(2*i)+1; %incrementing diffusive bin
end
% increment appropriate bin
end
%this is storing a value in one of the bins created
end
for n=1:beetles*behaviors
count(n)=behaviorSum(n)/nData;
end
end
% Show bar graph of outcome
bar(1:beetles*behaviors, count) %this is creating the histogram with 1:behaviors as number of bars and count as y values for bars
title(sprintf('Characteristic time \deltat_{c} vs. Probability', yValues), 'Fontsize',14)
xlabel('Characteristic time \deltat_{c}', 'Fontsize',14)
ylabel('Probability', 'Fontsize',14)
shg %show graph window
else
fprintf('Thanks for helping!');
end
end
figure
semilogy(my_test_data)

Sign in to comment.

Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!