필터 지우기
필터 지우기

Creating Variable names from strings

조회 수: 1 (최근 30일)
Sanjay
Sanjay 2012년 3월 4일
Hi: I am a new user so apologies for any stupid questions.
Here is what I am seeking to do
1. Read a XLS file with 1 column and 50 rows of ticker name 2. Go through this matrix in MATLAB cell by cell and create a variable in the MATLAB workspace that will hold the price data of this variable, pulled from yahoo.
I can do 1. However, I need help in achieving the "create variable" part of 2. I read and tried "genvarname" function, but still no success.
Can anyone here please help me with this.
Many thanx
  댓글 수: 1
Jan
Jan 2012년 3월 4일
Please post what you have done and explain "no success" with details.

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

답변 (3개)

Image Analyst
Image Analyst 2012년 3월 4일
For #1, simply call xlsread() like it sounds like you are doing.
Just keep your prices in the array. If your workbook has both the prices and tickername, you can go down the cell array and convert to an array of structures if you think that would be easier.
  댓글 수: 1
Sanjay
Sanjay 2012년 3월 4일
Thank you. I appreciate the heads up. Reading that FAQ now.
Best

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


Jan
Jan 2012년 3월 4일
I suggest not to use eval to create the variables directly in the current workspace, because this can conflict (beside other problems) with other variables, which are defined already.
Better store the variables as fields of a struct:
for iColumn = 1:n
S.(genvarname(Name{iColumn})) = Data{iColumn};
end
"Name" and "Data" depend on the format of your input data.

Rick Rosson
Rick Rosson 2012년 3월 4일
Don't worry about making them variable names. The easiest thing to do is to read the ticker symbols in as a cell array of strings. For more info:
>> doc xlsread
HTH.
  댓글 수: 5
Rick Rosson
Rick Rosson 2012년 3월 5일
Sanjay: Walter, Jan, Image Analyst, and I are all very experienced MATLAB programmers. All four of us have independently recommended against using the ticker symbols as variable names. We have each proposed various alternative ways to organize the data. You are free to do what you want, but please consider following our advice. Thanks!
Walter Roberson
Walter Roberson 2012년 3월 5일
Each time I take on a major project, I spend the first several *years* re-writing the code to eliminate the unsustainable code the previous generations of programmers had put in.
You know what that _feels_ like to a skilled and creative programmer working for years to clean up code? If feels like one has been sent to the basement to clean all of the walls and storage rooms and duct-work with a small wire tooth-brush, having to derive one's entire satisfaction out of knowing that one has postponed the critical rust and mold spots for another 5 years -- as no-one else will give a care beyond thinking you've been a waste of company salary.
So, please do graying maintenance programmers everywhere a favor and design your programs to make it easy for those who will come after you in debugging and enhancing and extending your programs.

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

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by