Clear Filters
Clear Filters

Definition of function

3 views (last 30 days)
VQ
VQ on 22 Mar 2011
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 Comment
Andrew Newell
Andrew Newell on 22 Mar 2011
What command are you using to run the code?

Sign in to comment.

Answers (1)

Matt Tearle
Matt Tearle on 22 Mar 2011
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 Comments
VQ
VQ on 22 Mar 2011
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 on 22 Mar 2011
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.

Sign in to comment.

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!