Debugging a piece of code
Show older comments
I have been trying to run a piece of code written in Matlab that contains the following:
function list = shellsort(list);
interval = floor(length(list)/3);
I get the following error message:
Not enough input arguments.
Error in shellsort (line 3) interval = floor(length(list)/3);
Any ideas on how to debug it?
Answers (2)
Walter Roberson
on 3 Nov 2020
0 votes
When you press the green Run button, what is your expectation about where MATLAB will search for the value of the variable named list ?
2 Comments
Federico Gremmo
on 3 Nov 2020
Edited: per isakson
on 28 Nov 2020
Walter Roberson
on 5 Nov 2020
You are pressing the green Run button to initiate the code. MATLAB needs to know the value of the variable named list in order to proceed. Where are you expecting MATLAB to look to find the variable named list?
Monika Jaskolka
on 3 Nov 2020
How are you running the shellsort function? From the error message you provided, it looks like you are not giving shellsort any input parameters
% Incorrect
>> shellsort
Not enough input arguments.
Error in shellsort (line 2)
interval = floor(length(list)/3);
% Correct
>> shellsort(list)
ans =
1 2 3
15 Comments
Federico Gremmo
on 3 Nov 2020
Steven Lord
on 3 Nov 2020
The error message is not coming from floor or length but that's the first place in your function you try to use the input argument (that doesn't exist.) So it is the first place MATLAB "notices" that you didn't provide enough input arguments.
Federico Gremmo
on 3 Nov 2020
Rik
on 3 Nov 2020
By providing an input argument, instead of using the green button. Just like Monika showed.
Federico Gremmo
on 3 Nov 2020
Monika Jaskolka
on 3 Nov 2020
You need to define the input argument first. In this case, it seems like you need a numerical array. Type the following into the command window, and press Enter. Obviously the list I give below is an example. You should define it according to your needs.
list = [1 3 2]
Then you need to run the function with the list as an input argument. Again, type it into the command window, and press Enter.
shellsort(list)
Federico Gremmo
on 3 Nov 2020
Monika Jaskolka
on 3 Nov 2020
Looks like you forgot the equal sign.
Federico Gremmo
on 3 Nov 2020
Federico Gremmo
on 3 Nov 2020
Monika Jaskolka
on 3 Nov 2020
I am able to run the script, but it does not do the sorting correctly. There are a few issues with the implementation itself.
Federico Gremmo
on 3 Nov 2020
Monika Jaskolka
on 3 Nov 2020
The problem is that that the shellsort function is implemented incorrectly. It outputs the array unchanged, because it is not doing the sorting. If you need a shellsort that works correctly, there is one already available to download on the File Exchange. If you need this particular shellshort, I encourage you to step-through and debug the program yourself. I was able to get the function working with 3 changes to the function.
When you press the "Run" button, Matlab essentially pastes the name of your script into the command window. The name of your script is "shellsort". If you try to run "shellsort" in the command window yourself, you will get the initial error you were inquiring about. "shellsort" is not good enough because the script also requires an input parameter, and you need to provide it. How else is the "Run" button supposed to know what the input is? For this reason, pressing the play button is not enough.
Federico Gremmo
on 5 Nov 2020
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!