How to define a string variable in IF statement
4 ビュー (過去 30 日間)
古いコメントを表示
Andrei Makarskiy
2015 年 10 月 15 日
コメント済み: the cyclist
2015 年 10 月 15 日
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
2015 年 10 月 15 日
編集済み: Andrei Makarskiy
2015 年 10 月 15 日
1 件のコメント
Star Strider
2015 年 10 月 15 日
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
2015 年 10 月 15 日
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 件のコメント
the cyclist
2015 年 10 月 15 日
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.
参考
カテゴリ
Help Center および File Exchange で Environment and Settings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!