Azzera filtri
Azzera filtri

Definition of function

3 visualizzazioni (ultimi 30 giorni)
VQ
VQ il 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 Commento
Andrew Newell
Andrew Newell il 22 Mar 2011
What command are you using to run the code?

Accedi per commentare.

Risposte (1)

Matt Tearle
Matt Tearle il 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 Commenti
VQ
VQ il 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 il 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.

Accedi per commentare.

Categorie

Scopri di più su Data Import from MATLAB in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by