Definition of function

4 vues (au cours des 30 derniers jours)
VQ
VQ le 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 commentaire
Andrew Newell
Andrew Newell le 22 Mar 2011
What command are you using to run the code?

Connectez-vous pour commenter.

Réponses (1)

Matt Tearle
Matt Tearle le 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 commentaires
VQ
VQ le 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 le 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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Import from MATLAB dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by