MATLAB Answers

How to define a string variable in IF statement

조회 수: 16(최근 30일)
I'm stuck with matlab on almost every simple step I used to work with pretty fast in PHP. How this piece of code inside my function makes me crazy:
matchStr = regexp(filename,'^([0-9\-])+-StockAndOptionQuoteFor([A-z]+)\.', 'tokens');
CurDate = matchStr{1,1}{1,1};
if(~Ticker)
Ticker = matchStr{1,1}{1,2};
end
Matlab keeps telling "Undefined function or variable 'Ticker'." or The variable 'Ticker' might be used before it is defined! But it's a standard practice in PHP. How do I know which type of variable it would be, a string or double?
Ticker='' didn't help

  댓글 수: 0

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

채택된 답변

Andrei Makarskiy
Andrei Makarskiy 15 Oct 2015
편집: Andrei Makarskiy 15 Oct 2015
global Ticker;
Inside the Function helped. But after the function stops running the global Ticker returns to '' in the workspace. I'm stuck.

  댓글 수: 1

Star Strider
Star Strider 15 Oct 2015
Using global variables is not considered good programming practice, and can cause problems. Add the variable to the function argument list instead.

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

추가 답변(1개)

the cyclist
the cyclist 15 Oct 2015
Spoiler alert: Different languages have different syntax. :-)
Are you trying to check if a variable named "Ticker" exists, and then create it if it does not? Then use the exist function
if ~exist('Ticker','var')
Ticker = matchStr{1,1}{1,2};
end
In MATLAB, "~" means logical negation.

  댓글 수: 2

Andrei Makarskiy
Andrei Makarskiy 15 Oct 2015
Thanks, Folks. Your advice helped. Moreover, I found the problem which set the Ticker to '' every time I called the function, since the statement global Ticker; reset the variable to empty. So, I took out this piece of code to a more appropriate place outside the function.
the cyclist
the cyclist 15 Oct 2015
This is an example of Star Strider's comment about defining global variables being a generally bad practice. Debugging problems can be a challenge, because code that is "far away" and difficult to find can affect something local.

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by