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

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

 Accepted 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.

14 Comments

test3dmatrix is just a separate file and all it does is have that one line of code that calls sensingamatrix3d
Well ... please show this code. Is it a function or a script? Where is it getting the "time" input to pass to your function?
this code is shown above as well
cubefinal = sensingmatrix3d(3);
There has to be something else you are not showing us. The script passes a constant 3 to your function, and your function doesn't change the input before displaying it, so there is no way it should be displaying anything other than 3 ... that is what happens when I run it. Are you sure you are calling that version of your function and/or script? What do you get when you type this:
which sensingmatrix3d
which test3dmatrix
Look at the difference between what the display and disp functions do:
>> display(time)
time =
3
>> disp(time)
3
You can't be calling the above code because disp would not be showing the "time =" part of your supposed output.
>> which sensingmatrix3d
C:\Users\Tara the Spud\Dropbox\Papers\sensingmatrix3d.m
>> which test3dmatrix
C:\Users\Tara the Spud\Dropbox\Papers\test3dmatrix.m
But are these the exact same code you shown above? And what about the display vs disp discrepancy in your output? Put breakpoints in both of the above files to make sure you are actually calling them. When you hit a breakpoint, step through the code to see what is happening.
now its telling me that sensingmatrix3d does not have enough inputs when i try to run it- why?
Somewhere in your code you likely executed a line without a "time" input for a sensingmatrix3d call. I strongly suggest you put in breakpoints in each file and step through everything in the debugger to see exactly what is being called with what inputs. If you haven't done this before, it is pretty easy. Just open up the file in the editor and click on the - symbol at the left of a line to create a breakpoint (a red dot will appear ... to eliminate the breakpoint just click on the red dot). When you run the code it will pause at this line. Then use the "step" button at the top to step through the code one line at a time. Also use the "step in" to step inside of function calls.
what do you mean i "executed a lined without a time input"? when i did the breakpoint thing, it stopped me at the line
if time == 3
and said i didn't have enough inputs
You need to set a breakpoint in your test3dmatrix script file first to make sure you are calling that code as expected.
it stops me at all of the lines where i do "time == x" and says i don't have enough inputs - what does it mean? what am i supposed to do?
i set the break point there and it gave all random values for time, again. what else should i try? thanks so much!
It means one of two things. Either you did not set the breakpoint in your test3dmatrix properly, or it means you are not calling the test3dmatrix file that you think you are calling. Check again what you get with the following:
which test3dmatrix
And then make sure you edit that exact file in the directory it shows. Then set a breakpoint in that exact file. Then make another run. It should pause at the line in test3dmatrix where you just set the breakpoint (i.e., the line where you call sensingmatrix3d).
thank you so much!! i have the correct file now :) and it works

Sign in to comment.

More Answers (1)

My crystal ball says you have a typo on line 23 of your function. Of course, my crystal ball is pretty unreliable...
Perhaps showing some code would help our divining?

Categories

Tags

Asked:

on 4 Aug 2015

Edited:

on 4 Aug 2015

Community Treasure Hunt

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

Start Hunting!