Is there a way to get the input argument index of a function?

25 views (last 30 days)
I am trying to read specific files from big database. Files are codenamed base on parameters Pn in a specific order within a range of tested values Vn: "Name_P1V1_P2V8_P3V4.csv"
To achieve this, I created a function that recieves vectors of specific values Vn to then load those specific datasets.
[OutputDB] = ReadFiles(P1,P2,P3);
I have a list of the specific values Vn that each parameter Pn could have, example for P1:
P1possible = [V1,V2,V3];
Since there are different parameters, values Vn for each Pn are unique. I thought of generating the address strings through a simple nested For
for P = 1:1:nargin
for V = 1: length(InputArgument(P)) %Input parameter vector length
switch P
% Input detection code with error handling
end
end
end
There are multiple ways of solving this issue like looping each parameter individually,I have read about varargin function and think that it could also be a solution, however I was wondering if there was a way to actually reference the input arguments individually.
I'm still learning how to program so this may be a silly question. If I should post this somewhere else, please let me know.
Thanks in advance for any help
  2 Comments
prasanth s
prasanth s on 11 Feb 2023
one can also store all input arguments in a cell array and pass the array to the function.
Rik
Rik on 11 Feb 2023
Perhaps varargin would help, although I must admit that I don't fully understand what you want.

Sign in to comment.

Accepted Answer

Nehemiae
Nehemiae on 7 Mar 2023
Hello,
In order to index each parameter of a variable input argument list to a function, “varargin” can be used. This is an input cell array that can be indexed for each of the Pn values passed to the function. For the example below, I have assumed that each Pn contains an array for the Vn indexes that are needed. Also, the strings are generated only for the first Vn of each Pn. This can be extended to generate the addresses for all the possible combinations as required.
% Possible Vn values for each Pn
P1 = [6, 7];
P2 = [1, 5, 4];
P3 = [2 1];
ReadFiles(P1, P2, P3);
ans = "Name_P1V6_P2V1_P3V2.csv"
function OutputDB = ReadFiles(varargin)
P = varargin; % Assuming that every element in cell array P, contains the required Vn for the corresponding Pn
numFiles = prod(cellfun(@(x) length(x), P)); % Assuming total required file names is all possible combinations of Pn and Vn
fileNames = strings(1, numFiles);
fileNames(:) = 'Name_';
for i = 1 : nargin
fileNames(1) = strcat(fileNames(1), 'P', num2str(i), 'V', num2str(P{i}(1))); % Concatenating Pn and its first possible Vn
if i ~= nargin
fileNames(1) = strcat(fileNames(1), '_');
end
end
fileNames(:) = strcat(fileNames(:), '.csv');
fileNames(1)
end
The documentation on varargin (https://www.mathworks.com/help/matlab/ref/varargin.html) and cell array indexing (https://www.mathworks.com/help/matlab/matlab_prog/access-data-in-a-cell-array.html) is useful in understanding the above code.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!