Write a function with input .xls file to read'

Hi, I'm writing a function (not a '.m' script) that allows me to make a series of econometric tests on a pair of stocks. My input should be the name of the excel file to read. More specifically, my idea is: Function Econometric= Analysis(Stock). The following first line of the code that I wrote was Stock=xlsread('nameofstock.xls',1,'B2:B263'). Unfortunately it doesn't work. I know all other following lines are ok but without first line...everything is useless. Can you help me? Thanks

답변 (2개)

Stephen23
Stephen23 2016년 7월 6일
편집: Stephen23 2016년 7월 6일

0 개 추천

function Econometric = Analysis(filename)
Stock = xlsread(filename,1,'B2:B263')
... the rest of your code
end
As explained in the function documentation, and also covered in the introductory tutorials:

댓글 수: 2

polar
polar 2016년 7월 6일
Yes I tried this but it shows this error: "Undefined function 'analysis' for input arguments of type 'char'.
Stephen23
Stephen23 2016년 7월 6일
편집: Stephen23 2016년 7월 6일
MATLAB is case sensitive. Your function is called Analysis, but your error message tells us that you are calling analysis. You need to make up your mind which one you are going to use!

댓글을 달려면 로그인하십시오.

Star Strider
Star Strider 2016년 7월 6일
편집: Star Strider 2016년 7월 6일

0 개 추천

The xlsread function is going to assume that ‘nameofstock.xls’ in this line is the file name you want to read, because of the single quotes:
Stock=xlsread('nameofstock.xls',1,'B2:B263');
If ‘nameofstock.xls’ is already a string, you don’t need the quotes.
----------------------------
EDIT ‘Yes I tried this but it shows this error: "Undefined function 'analysis' for input arguments of type 'char'.’
You named your function ‘Analysis’ but called it as ‘analysis’. MATLAB is case-sensitive, so ‘Analysis’ is not the same as ‘analysis’.
Call it as:
Econometric= Analysis(Stock);
and you will not get that error.

댓글 수: 4

polar
polar 2016년 7월 6일
uhmmm..I think I'm missing you. Ok maybe I had to tell this first: I don't run the function script but save it and then, on the COMMAND WINDOW, I write: Analysis('filenanem.xls'). So I think I'm wrong here...what to do?
It depends on how you wrote your function.
I don’t have all your code, but you could be creating a conflict with:
Function Econometric = Analysis(Stock)
and:
Stock=xlsread('nameofstock.xls',1,'B2:B263')
That would replace your input variable ‘Stock’ with the result of the file read operation.
I would do something similar to:
StockData = xlsread(Stock,1,'B2:B263');
instead.
polar
polar 2016년 7월 6일
YESSSSS I DID!!!! MY mistake: I previoulsy changed the name of the function from "analysis" to another name and obviously Matlab didn't recognized it. Thanks mate!
My pleasure!

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Surrogate Optimization에 대해 자세히 알아보기

제품

질문:

2016년 7월 6일

댓글:

2016년 7월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by