Calling the size of an input

4 visualizaciones (últimos 30 días)
Philip Berg
Philip Berg el 18 de En. de 2017
Editada: Philip Berg el 18 de En. de 2017
I call different models in a function. I want to call the size of the inputs (number of parameters the model takes) from the function I'm calling them. E.g.,
function y = model1(t,x,Pm,Other_Inputs)
Some stuff = Pm(1);
Some other stuff = Pm(2);
...
More stuff = Pm(n);
%some stuff it does using the inputs.
end
Now I want to call it with something like
function x = caller(Model)
%For example
P = size(Pm,1)
z = randi(P,1));
[~,yx2] = ode15s(@(t,x) model1...
(t,x,z,Other_Inputs)t,InitalValue);
end
Is this possible? I know some basics about varargin and nargin, but I'm not sure if they are what I should use here. Thanks for any help.
  2 comentarios
Jan
Jan el 18 de En. de 2017
I do not understand the question. What is "P" in the caller? Where is the problem? Which step does not work?
Philip Berg
Philip Berg el 18 de En. de 2017
Editada: Philip Berg el 18 de En. de 2017
Ahh... Sorry let me try to clarify. I have some different models that take different amount of parameters, P. In other words, P is a row vector. Now, from my main function I will call some model that is given as a input, but for now I also have to specify how many parameters the model takes as an input. I want this to be automated, so when I call a model, the function figures out the size of P instead of me giving it as an input. The problem is that I don't know how to get the size of P in the caller function. What I want to know is n from P(n) in the model. Do I make more sense?

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 18 de En. de 2017
‘I also have to specify how many parameters the model takes as an input.’
If ‘Pm’ is a vector and all the parameters are in the same order, the models themselves can decide.
For example:
function out1 = model1(t,x,Pm,Other_Inputs)
N = 3;
P1 = Pm(1:N);
. . .
end
function out2 = model2(t,x,Pm,Other_Inputs)
N = 5;
P2 = Pm(1:N);
. . .
end
This assumes I understand what you want to do.
  4 comentarios
Philip Berg
Philip Berg el 18 de En. de 2017
Editada: Philip Berg el 18 de En. de 2017
Yes, I know eval etc slows down the script. I just want to randomize n numbers. Where n is the number of parameters the model being called by the main function. But I would like that number to be given from how many parameters the model takes. I'm not sure how to make this clearer. The only way I figured it out is to put an output vector of zeroes that corresponds to the number of parameters and then do size on that vector. Edit: Oh and thank you for the help---of course!
Star Strider
Star Strider el 18 de En. de 2017
My pleasure!

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 18 de En. de 2017
So you want to turn some unknown number of parameters into individual inputs of another function? In this case I would use a cell array and turn it into a comma-separated list when you call the other function. Put the following code in a file named helper.m:
function helper(vectorOfParameters)
cellOfParameters = num2cell(vectorOfParameters);
showParameters(cellOfParameters{:});
function showParameters(varargin)
for whichParameter = 1:nargin
fprintf('Parameter %d is %g.\n', whichParameter, varargin{whichParameter});
end
Try calling the main function like this:
helper(1:5)
helper([99 4334 237 1 55 999 -17 6])
Inside helper, the num2cell function accepts the vector of parameters and creates a cell array of the same length with each element of the vector stored in the corresponding cell of the cell array. When I call showParameters, I use this syntax: cellOfParameters{:} which is just like:
showParameters(cellOfParameters{1}, cellOfParameters{2}, cellOfParameters{3}, ...)
except I don't have to write out all 1, 2, 3, 20, etc. parameters in the function call. Then inside showParameters I iterate over the number of inputs MATLAB passed into the function (which the nargin function returns) and use the loop variable to index into the cell array varargin.
  1 comentario
Philip Berg
Philip Berg el 18 de En. de 2017
Editada: Philip Berg el 18 de En. de 2017
Well I see that I'm not very good at explaining my problem, sorry still a matlab novice... "So you want to turn some unknown number of parameters into individual inputs of another function?" Well, the number of parameters is know inside the models. Its the function that calls the model that wants to know how many they are. So when you call a model that has 5 parameters instead of 4---you just specify the name of model as input, and then in accordance with the number of parameters the model has. The main function (the one calling the model) will then randomizes that many. So say Model1 takes 3 parameters, and model2 take 5, then MainFunction(Model1) should give 3 random numbers while MainFunction(Model2) would give 5 random numbers.

Iniciar sesión para comentar.

Categorías

Más información sobre Data Representation in Generated Code en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by