MATLAB: Error message “Error: Function definitions are not permitted in this context.” for function with matrix

errorfunctionmatrix

I'm reading in a 5×5 matrix from excel and a 1×5 vector from excel. I then need to multiply them together in a function. Unfortunately it must be in a function. I keep getting an error message of "Error: Function definitions are not permitted in this context." I'm not quite sure why I'm getting this error. Any help would be appreciated.
Here's my code so far
%%Read in data
A=xlsread('AandB','a:e')
b=xlsread('AandB','g:g')
%%Multiply A and B
function [x] = multiply(A,b)
x=A*b
end

Best Answer

If you want the function and the test code all in the same m-file, you can but you need to put a function line at the beginning with the name of the m-file. Like if your m-file is called test.m, then inside that single file have all of this (both functions):
function test()
%%Read in data
A=xlsread('AandB','a:e')
b=xlsread('AandB','g:g')
%%Multiply A and B
x = multiply(A, b)
end
function [x] = multiply(A,b)
x=A*b;
end
Related Question