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?

답변 (2개)

Walter Roberson
Walter Roberson 2020년 11월 3일

0 개 추천

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

Federico Gremmo
Federico Gremmo 2020년 11월 3일
편집: per isakson 2020년 11월 28일
This is the whole code. however, I get the error message on the line of the line "interval = floor(length(list)/3);"; so I take it that Matlab thinks there is something wrong with the floor function.
function list = shellsort(list);
interval = floor(length(list)/3);
while (interval > 0)
for i = interval:length(list)
temp = list(i);
j = i;
while ((j < interval) & (list(j - interval) > temp))
list(j - interval) = list(j);
j = j - interval;
end % while
temp = list(j);
end %for
interval = floor(interval/2);
end % while
end % shellsort
Walter Roberson
Walter Roberson 2020년 11월 5일
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
Monika Jaskolka 2020년 11월 3일

0 개 추천

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

Federico Gremmo
Federico Gremmo 2020년 11월 3일
But, that's not what Matlab is complaining about. It's complaining about the floor function because that's where the error message is.
Steven Lord
Steven Lord 2020년 11월 3일
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
Federico Gremmo 2020년 11월 3일
Ok. So, how do I correct it?
Rik
Rik 2020년 11월 3일
By providing an input argument, instead of using the green button. Just like Monika showed.
Federico Gremmo
Federico Gremmo 2020년 11월 3일
So, should type the the input argument in the command window?
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
Federico Gremmo 2020년 11월 3일
Sorry to be a pain, but when I type list = [1 3 2] into the command window and I click enter, I get an error message.
list[1 3 2]
Error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for
mismatched delimiters.
Monika Jaskolka
Monika Jaskolka 2020년 11월 3일
Looks like you forgot the equal sign.
Federico Gremmo
Federico Gremmo 2020년 11월 3일
Thank you.
Federico Gremmo
Federico Gremmo 2020년 11월 3일
The script still doesn't run, but thank you anyway.
Monika Jaskolka
Monika Jaskolka 2020년 11월 3일
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
Federico Gremmo 2020년 11월 3일
Any ideas what those issues might be? Because when I do as you say, it simply returns the array unchanged. It looks like the script is not running at all. And it doesn't give an error message. By the way, why is it not possible to run the script with the green button?
Monika Jaskolka
Monika Jaskolka 2020년 11월 3일
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
Federico Gremmo 2020년 11월 5일
I need to use this particular one. The problem is that I have never used Matlab before, so I do not know how to step through the program. If you could share the three changes with me, it would be great. If not, too bad.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2020년 11월 3일

편집:

2020년 11월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by