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

49 次查看(过去 30 天)
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 个评论
prasanth s
prasanth s 2023-2-11
one can also store all input arguments in a cell array and pass the array to the function.
Rik
Rik 2023-2-11
Perhaps varargin would help, although I must admit that I don't fully understand what you want.

请先登录,再进行评论。

采纳的回答

Nehemiae
Nehemiae 2023-3-7
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by