Definition of function

3 次查看(过去 30 天)
VQ
VQ 2011-3-22
Hi, I made a function to read data from Excel. I wrote this code and saved it as a .m file with the name of readExcel.m
function [a] = readExcel(filename) a=xlsread(filename); end
When I run the code I get this message:
??? function [a] = readExcel(filename) | Error: Function definitions are not permitted in this context.
What's the problem?
Thanks.

回答(1 个)

Matt Tearle
Matt Tearle 2011-3-22
Are there any lines of code above the function definition line? That will generate that error message. For example:
filename = 'foo';
function [a] = readExcel(filename)
a=xlsread(filename);
end
In the Command Window:
>> readExcel('myfile')
??? Error: File: readExcel.m Line: 2 Column: 1
Function definitions are not permitted in this context.
A function file must contain nothing but that function (and any subfunctions below it).
As an aside: why make a function that just calls a built-in MATLAB function? Just curious.
  2 个评论
VQ
VQ 2011-3-22
No, there aren't any lines of code above the function definition line.
Well, my function is not only designed to read data from excel. I want to open the .xls file and work with the data in the differents columns.
Thank you.
Matt Tearle
Matt Tearle 2011-3-22
Can you please do a direct, exact copy-&-paste of (1) your file, (2) the command you used to call it, and (3) the error message. You can edit your question and use the markup options to format your code as code.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Import from MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by