Why do i keep on getting error 'Not enough input arguments' on calling the function?

3 Ansichten (letzte 30 Tage)
I want to create a table of values of certain lengths (indices stored as indices )from a vector. For that I create an empty table and then later insert these values inside that. This works in the code, but when I try to make a function, it gives an error, "Not enough input arguments."
The working code is given below
clc
clear all
close all
Data= load('array.mat');
array = Data.array
indices = [4 80 99 143 166 197 204 250 295 350 385 400 467 554];
lengthx = length(indices);
lengthy = height(array);
extracted = NaN([lengthy lengthx]);
extracted = array2table(extracted);
for i = 1:lengthx
num = indices(i);
a = array(1:num);
padding = lengthy-height(a);
extracted(:,i)= array2table(padarray(a,padding,NaN,'post'));
end
Now, I try to make a function out of it as
function [extracted] = arrayextraction(Data,indices)
lengthx = length(indices);
lengthy = height(Data);
extracted= NaN([lengthy lengthx]);
extracted= array2table(cumx);
for i = 1:lengthx
num = indices(i);
a = Data(1:num);
padding = lengthy-height(a);
extracted(:,i)= array2table(padarray(a,padding,NaN,'post'));
end
end
When I call the function using the following code, it gives an error of 'Not enough input arguments'. The only difference is something to do with the indices. When I put the indices vector inside the function, it works, but when I put it as input, it gives error. I call the function using the following code
Data= load('array.mat');
array = Data.array
b = arrayextraction(Data);
I ran through the help and documentation, it may be a simple misunderstanding that i cannot figure out and I will appreciate help in this regard.
Thanks
Good day.

Akzeptierte Antwort

Karim
Karim am 31 Okt. 2022
See below for the modified version.
There were two issues:
  1. You need to call the function with two inputs: the array and the indices. You only provide the array, hence the error.
  2. In the routine you changed a varibale name in the line extracted = array2table(extracted)
% load the mat file
Data = load(websave('myFile', "https://nl.mathworks.com/matlabcentral/answers/uploaded_files/1174743/array.mat"));
% get the inputs
array = Data.array;
indices = [4 80 99 143 166 197 204 250 295 350 385 400 467 554];
% run the function
[extracted] = arrayextraction(array,indices);
% have a look at the ouput
extracted
extracted = 554×14 table
extracted1 extracted2 extracted3 extracted4 extracted5 extracted6 extracted7 extracted8 extracted9 extracted10 extracted11 extracted12 extracted13 extracted14 __________ __________ __________ __________ __________ __________ __________ __________ __________ ___________ ___________ ___________ ___________ ___________ 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 1384 NaN 1103 1103 1103 1103 1103 1103 1103 1103 1103 1103 1103 1103 1103 NaN 1103 1103 1103 1103 1103 1103 1103 1103 1103 1103 1103 1103 1103 NaN 1103 1103 1103 1103 1103 1103 1103 1103 1103 1103 1103 1103 1103 NaN 1103 1103 1103 1103 1103 1103 1103 1103 1103 1103 1103 1103 1103 NaN 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 NaN 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 NaN 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 NaN 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 936.9 NaN 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 NaN 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 NaN 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 NaN 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4 916.4
% this is the modified function
function [extracted] = arrayextraction(Data,indices)
lengthx = length(indices);
lengthy = height(Data);
extracted = NaN(lengthy,lengthx);
extracted = array2table(extracted);
for i = 1:lengthx
num = indices(i);
a = Data(1:num);
padding = lengthy-height(a);
extracted(:,i)= array2table(padarray(a,padding,NaN,'post'));
end
end

Weitere Antworten (0)

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by