MATLAB: Can somebody tell me why, when I call a function using an input, the input is disregarded

matrix

I have a function,
function cubefinal = sensingmatrix3d(time)
and I call it like this if I want time=3:
cubefinal = sensingmatrix3d(3);
but it is not working (the body of the function is correct) because when I displayed 'time', answers seemed totally random:
>> test3dmatrix
time =
6
>> test3dmatrix
time =
6
>> test3dmatrix
time =
7
>> test3dmatrix
time =
7
>> test3dmatrix
time =
4
>> test3dmatrix
time =
5
could somebody please tell me why time is random? and how to fix it? thank you.
Edit: sorry — here is my code
function cubefinal = sensingmatrix3d(time)
exposure=zeros(60,96,10);
if time==3
for j=1:size(exposure,1);
for jj=1:size(exposure,2);
randnum=randi([1 8]);
exposure(j,jj,randnum:randnum+2)=1;
%if rand < 0.003
% checkRow=reshape(exposure(j,jj,:), [1 10])
%end
end
end
elseif time == 4
for j=1:size(exposure,1);
for jj=1:size(exposure,2);
randnum=randi([1 7]);
exposure(j,jj,randnum:randnum+3)=1;
end
end
elseif time == 5
for j=1: size(exposure,1);
for jj=1:size(exposure,2);
randnum=randi([1 6]);
exposure(j,jj,randnum:randnum+4)=1;
end
end
elseif time == 6
for j=1:size(exposure,1);
for jj=1:size(exposure,2);
randnum=randi([1 5]);
exposure(j,jj,randnum:randnum+5)=1;
end
end
elseif time == 7
for j=1:size(exposure,1);
for jj=1:size(exposure,2);
randnum=randi([1 4]);
exposure(j,jj,randnum:randnum+6)=1;
end
end
end
cubefinal=exposure;
disp(time)
end

Best Answer

You show code for function sensingmatrix3d, but your example is calling test3dmatrix. How are the two related? Look at how test3dmatrix is calling your function.