MATLAB not enough input arguments - matlab

I keep trying to run this and have no idea what is going wrong. I have it saved as test.m. I click run in the editor and in the matlab command window, it states not enough input arguments. I feel like I am missing something totally obvious, but I cannot spot the issue.
function y = test(A, x)
%This function computes the product of matrix A by vector x row-wise
% define m number of rows here to feed into for loop
[ma,na] = size(A);
[mx,nx] = size(x);
% use if statement to check for proper dimensions
if(na == mx && nx == 1)
y = zeros(ma,1); % initialize y vector
for n = 1:ma
y(n) = A(n,:)*x;
end
else
disp('Dimensions of matrices do not match')
y = [];
end
end

It is a function (not an script) and it needs some input arguments to run (in this case A and x), so you cannot hit the run button and expect it to run.
The first way:
Instead you can use the command windows in MATLAB and enter the command:
A = rand(3,3); % define A here
x = ones(3,1); % define x here
test(A,x) % then run the function with its arguments
remember that A and x should be defined properly.
The second way is:
Also you can hit the little triangle besides the green run button (see the figure below), and it will show you another option, type command to run. And
there you can directly enter the same command test(A,x). After that, each time you just hit enter for this function and it runs this command instead of only the test command without any argument.

The third way:
function y = test(A, x)
%// TESTING CODE:
if nargin==0
A = default_value_for_A;
x = default_value_for_x;
end
... %// rest of the function code
This way allows you to "click the play button" and have your function run with no explicit input arguments. However, be advised that such a method should only be used:
When debugging, so as not to allow users to call the function with no arguments if this is not its intended use case.
If your function is not supposed to behave differently for a different number of inputs. See also function overloading in MATLAB based on number of input arguments.

Related

No output while using "gmres" in Matlab

I want to solve a system of linear equalities of the type Ax = b+u, where A and b are known. I used a function in MATLAB like this:
x = #(u) gmres(A,b+u);
Then I used fmincon, where a value for u is given to this expression and x is computed. For example
J = #(u) (x(u)' * x(u) - x^*)^2
and
[J^*,u] = fmincon(J,...);
withe the dots as matrices and vectors for the equalities and inequalities.
My problem is, that MATLAB delivers always an output with information about the command gmres. But I have no idea, how I can stop this (it makes the Program much slower).
I hope you know an answer.
Patsch
It's a little hidden in the documentation, but it does say
No messages are displayed if the flag output is specified.
So you need to call gmres with at least two outputs. You can do this by making a wrapper function
function x = gmresnomsg(varargin)
[x,~] = gmres(varargin{:});
end
and use that for your handle creation
x = #(u) gmresnomsg(A,b+u);

Arbitrary dimension for CVXGEN program

I would like to solve a QP/LP problem in MatLab using CVXGEN. I have preference for CVXGEN over CVX, since CVXGEN is much faster. In particular, I would like to solve
min f(x) s.t. x in X
where f(x) is in quadratic form and X is compact, convex, and defined by linear functions. The size of the problem varies depending on the run. I would like to automate the procedure as much as possible. To illustrate, an example of a CVXGEN code is:
dimensions
n = 10
end
parameters
Q (n,n) psd # quadratic penalty.
end
variables
x (n)
end
minimize
quad(x, Q)
end​​​​​​​​​​​​​
​This code is inputted at cvxgen.com. On this website, I can then generate the C code which gives me a unique number. I can then compile this into MEX code using the unique number. Last, I can call this MEX code (csolve) from MatLab by running the following code
n=10; % dimension of the problem
params.Q = eye(n,n); % assume that the Hessian is the identity
[vars, status] = csolve(params); % this outputs optimal x* = 0.
This procedure, however, requires for each dimension of the problem n that I want to run, I need to go to cvxgen.com, change n, compile code, then run my MatLab code. Is it possible to let n enter as a parameter? This way, I only need to compile the code once, then in my MatLab code set params.n = n and params.Q = eye(n,n), and then call [vars, status] = csolve(params);.
In short, I do not think it is possible to specify arbitrary dimension on cvxgen.com. I do have one solution: First, in MatLab, allow the user to specify his/her email address and password for cvxgen.com. Next, using system command in MatLab, call a python or javascript program that executes the following steps:
1) Log user into cvxgen.com
2) Goto edit tab on website
3) Copy and paste the cvxgen code
4) Goto generate c tab
5) Click generate code
6) Goto matlab tab
7) Copy unique identifying number for the compiled C code to be downloaded
8) Pass the unique identifying number back to MatLab and compile the Mex files in MatLab by calling the identifying number.
Now the Mex files can be called in MatLab by the following commands:
n=10; % dimension of the problem
params.Q = eye(n,n); % assume that the Hessian is the identity
[vars, status] = csolve(params); % this outputs optimal x* = 0.

Matlab r2016a throwing error [duplicate]

I keep trying to run this and have no idea what is going wrong. I have it saved as test.m. I click run in the editor and in the matlab command window, it states not enough input arguments. I feel like I am missing something totally obvious, but I cannot spot the issue.
function y = test(A, x)
%This function computes the product of matrix A by vector x row-wise
% define m number of rows here to feed into for loop
[ma,na] = size(A);
[mx,nx] = size(x);
% use if statement to check for proper dimensions
if(na == mx && nx == 1)
y = zeros(ma,1); % initialize y vector
for n = 1:ma
y(n) = A(n,:)*x;
end
else
disp('Dimensions of matrices do not match')
y = [];
end
end
It is a function (not an script) and it needs some input arguments to run (in this case A and x), so you cannot hit the run button and expect it to run.
The first way:
Instead you can use the command windows in MATLAB and enter the command:
A = rand(3,3); % define A here
x = ones(3,1); % define x here
test(A,x) % then run the function with its arguments
remember that A and x should be defined properly.
The second way is:
Also you can hit the little triangle besides the green run button (see the figure below), and it will show you another option, type command to run. And
there you can directly enter the same command test(A,x). After that, each time you just hit enter for this function and it runs this command instead of only the test command without any argument.
The third way:
function y = test(A, x)
%// TESTING CODE:
if nargin==0
A = default_value_for_A;
x = default_value_for_x;
end
... %// rest of the function code
This way allows you to "click the play button" and have your function run with no explicit input arguments. However, be advised that such a method should only be used:
When debugging, so as not to allow users to call the function with no arguments if this is not its intended use case.
If your function is not supposed to behave differently for a different number of inputs. See also function overloading in MATLAB based on number of input arguments.

Error message: Not enough input arguments [duplicate]

I keep trying to run this and have no idea what is going wrong. I have it saved as test.m. I click run in the editor and in the matlab command window, it states not enough input arguments. I feel like I am missing something totally obvious, but I cannot spot the issue.
function y = test(A, x)
%This function computes the product of matrix A by vector x row-wise
% define m number of rows here to feed into for loop
[ma,na] = size(A);
[mx,nx] = size(x);
% use if statement to check for proper dimensions
if(na == mx && nx == 1)
y = zeros(ma,1); % initialize y vector
for n = 1:ma
y(n) = A(n,:)*x;
end
else
disp('Dimensions of matrices do not match')
y = [];
end
end
It is a function (not an script) and it needs some input arguments to run (in this case A and x), so you cannot hit the run button and expect it to run.
The first way:
Instead you can use the command windows in MATLAB and enter the command:
A = rand(3,3); % define A here
x = ones(3,1); % define x here
test(A,x) % then run the function with its arguments
remember that A and x should be defined properly.
The second way is:
Also you can hit the little triangle besides the green run button (see the figure below), and it will show you another option, type command to run. And
there you can directly enter the same command test(A,x). After that, each time you just hit enter for this function and it runs this command instead of only the test command without any argument.
The third way:
function y = test(A, x)
%// TESTING CODE:
if nargin==0
A = default_value_for_A;
x = default_value_for_x;
end
... %// rest of the function code
This way allows you to "click the play button" and have your function run with no explicit input arguments. However, be advised that such a method should only be used:
When debugging, so as not to allow users to call the function with no arguments if this is not its intended use case.
If your function is not supposed to behave differently for a different number of inputs. See also function overloading in MATLAB based on number of input arguments.

MATLAB: Not enough input arguments [duplicate]

I keep trying to run this and have no idea what is going wrong. I have it saved as test.m. I click run in the editor and in the matlab command window, it states not enough input arguments. I feel like I am missing something totally obvious, but I cannot spot the issue.
function y = test(A, x)
%This function computes the product of matrix A by vector x row-wise
% define m number of rows here to feed into for loop
[ma,na] = size(A);
[mx,nx] = size(x);
% use if statement to check for proper dimensions
if(na == mx && nx == 1)
y = zeros(ma,1); % initialize y vector
for n = 1:ma
y(n) = A(n,:)*x;
end
else
disp('Dimensions of matrices do not match')
y = [];
end
end
It is a function (not an script) and it needs some input arguments to run (in this case A and x), so you cannot hit the run button and expect it to run.
The first way:
Instead you can use the command windows in MATLAB and enter the command:
A = rand(3,3); % define A here
x = ones(3,1); % define x here
test(A,x) % then run the function with its arguments
remember that A and x should be defined properly.
The second way is:
Also you can hit the little triangle besides the green run button (see the figure below), and it will show you another option, type command to run. And
there you can directly enter the same command test(A,x). After that, each time you just hit enter for this function and it runs this command instead of only the test command without any argument.
The third way:
function y = test(A, x)
%// TESTING CODE:
if nargin==0
A = default_value_for_A;
x = default_value_for_x;
end
... %// rest of the function code
This way allows you to "click the play button" and have your function run with no explicit input arguments. However, be advised that such a method should only be used:
When debugging, so as not to allow users to call the function with no arguments if this is not its intended use case.
If your function is not supposed to behave differently for a different number of inputs. See also function overloading in MATLAB based on number of input arguments.