Filter löschen
Filter löschen

Definition of function

3 Ansichten (letzte 30 Tage)
VQ
VQ am 22 Mär. 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 Kommentar
Andrew Newell
Andrew Newell am 22 Mär. 2011
What command are you using to run the code?

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Matt Tearle
Matt Tearle am 22 Mär. 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 Kommentare
VQ
VQ am 22 Mär. 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 am 22 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Import from MATLAB finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by