Why do I receive error "Not enough input Argument"? Error in CreatePoints (line 7) plotPoints = linspace(lowValue, highValue, 5);

3 visualizaciones (últimos 30 días)
Hello, I writing a code to Linear-spaced points array. function. But I have a problem which is "not enough Arguments" More specifly error in Error in CreatePoints (line 7)
plotPoints = linspace(lowValue, highValue, 5);
.I am not sure if you know what is the problem,I have paste my written codes below.
Linear-spaced points array.
Construct a row array plotPoints with 5 values that are spaced linearly from lowValue to highValue. Hint: Use the linspace function.
Ex: If lowValue is 1 and highValue is 10, plotPoints is [1.0000, 3.2500, 5.5000, 7.7500, 10.0000]
function plotPoints = CreatePoints(lowValue, highValue)
% lowValue: Starting value in plotPoints
% highValue: Ending value in plotPoints
% Construct a row array plotPoints with 5 linear-spaced
% point from lowValue to highValue
plotPoints = linspace (lowValue, highValue,5);
end

Respuesta aceptada

Umar
Umar el 15 de Oct. de 2024

Hi @Eve,

The error message "not enough input arguments" typically indicates that the function CreatePoints is being called without the required parameters, lowValue and highValue. In MATLAB, when you define a function that requires input arguments, you must provide those arguments when calling the function. Let me break down the solution step-by-step, making sure that you have a complete understanding of how to implement this correctly.

Step 1: Define the Function

You have already defined the function correctly. Here’s the code you provided, which is mostly correct:

% Define the function
function plotPoints = CreatePoints(lowValue, highValue)
  % lowValue: Starting value in plotPoints
  % highValue: Ending value in plotPoints
  % Construct a row array plotPoints with 5 linear-spaced 
  % points from lowValue to highValue
  plotPoints = linspace(lowValue, highValue, 5);
end

Step 2: Calling the Function

To call this function correctly, you need to provide the lowValue and highValue arguments. You can do this in the MATLAB command window or in a separate script (M-file). Here’s how you can call the function:

% Call the function with example values
pts = CreatePoints(1, 10);

Step 3: Complete Example

To ensure that everything works seamlessly, here’s a complete example that includes both the function definition and the function call. You can place this code in a single M-file or run it in the command window:

% Define the function
function plotPoints = CreatePoints(lowValue, highValue)
  % lowValue: Starting value in plotPoints
  % highValue: Ending value in plotPoints
  % Construct a row array plotPoints with 5 linear-spaced 
  % points from lowValue to highValue
  plotPoints = linspace(lowValue, highValue, 5);
end
% Call the function with example values
pts = CreatePoints(1, 10);
% Display the result
disp(pts);

Please see attached.

For more information on linspace function, please refer to

https://www.mathworks.com/help/matlab/ref/double.linspace.html

Make sure that you are not trying to run the function directly by clicking the green "Run" button in the MATLAB editor without providing the necessary input arguments. Instead, you should run the function from the command window or include the function call in a script. If you continue to encounter issues, double-check that the function is saved in a file named GeneratePoints.m and that you are calling it from the same directory or have the directory added to your MATLAB path.By following these steps, you should be able to resolve the error and successfully create a linear-spaced points array in MATLAB.

Also, @Walter Roberson provided some good points regarding to your comments especially, “Note that the overall code could not be named CreatePoints.m (that is, a script may not be named the same as a function inside the script.)”

Hope this helps.

If you have any further questions or need additional assistance, feel free to ask!

  4 comentarios
Umar
Umar el 15 de Oct. de 2024
Hi @Eve,
Glad to know it worked out for you. Please don’t forget to click “Accept Answer” and provide vote to @Voss and @Walter Roberson for their efforts.

Iniciar sesión para comentar.

Más respuestas (1)

Voss
Voss el 15 de Oct. de 2024
Editada: Voss el 15 de Oct. de 2024
You need to provide inputs to the function when you run it, i.e., you cannot just click on the green Run button because that does not supply any inputs to the function.
Example, in the command window (or in a separate m-file):
pts = CreatePoints(1,10)
pts = 1×5
1.0000 3.2500 5.5000 7.7500 10.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
  2 comentarios
Eve
Eve el 15 de Oct. de 2024
@Voss, I added pts pts = CreatePoints(1,10) in the .m file. An It is still giving me, Not enough input Argument"? Error in CreatePoints (line 7) plotPoints = linspace(lowValue, highValue, 5);
Walter Roberson
Walter Roberson el 15 de Oct. de 2024
Editada: Walter Roberson el 15 de Oct. de 2024
You have to put the CreatePoints call at the correct place.
pts = CreatePoints(1,10)
pts = 1×5
1.0000 3.2500 5.5000 7.7500 10.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
function plotPoints = CreatePoints(lowValue, highValue)
% lowValue: Starting value in plotPoints
% highValue: Ending value in plotPoints
% Construct a row array plotPoints with 5 linear-spaced
% point from lowValue to highValue
plotPoints = linspace (lowValue, highValue,5);
end
Note that the overall code could not be named CreatePoints.m (that is, a script may not be named the same as a function inside the script.)

Iniciar sesión para comentar.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by