Error; Unbalanced or unexpected parenthesis or bracket.
5 次查看(过去 30 天)
显示 更早的评论
I am working on a program and while writting one of the functions (see below) I keep on getting the following error: Error: File: trendcount.m Line: 9 Column: 22 Unbalanced or unexpected parenthesis or bracket. I cannot figure out what the problem is, as the brackets I am using are meant for indexing the elements in the vector in question. Any ideas how to fix this?
function [short_dur,long_dur, counter] = trendcount(index)
%This function counts the number of days the index was trending
% The index specifies a file where the data of index pricing is, you need
% to import only one line vector.
% is present.
i=0,j=0,k=0
while numel(index) >= k
if index(k) >= 0
for index(k+1) >= 0
i=i+1;
k=k+1;
end
counter(j)= i
j=j+1;
i=0
else index(k) < 0
forindex(k+1)< 0
i=i+1;
k=k+1;
end
counter(j)=i
j=j+1
i=0
end
end
% Finding the shortest trend
short_dur = min(counter)
%Finding the lonest trend
long_dur = max(counter)
回答(1 个)
John D'Errico
2015-8-8
编辑:John D'Errico
2015-8-8
You may think this is good MATLAB syntax, that it does what you expect. It probably won't.
for index(k+1) >= 0
Maybe you wanted to use a while instead of a for, though I see that you know about the while loop. Maybe you wanted to use an if. I can't tell what you wanted.
The for statement is used to define a loop with explicit start and end points in the loop. It defines a variable.
And in this line:
forindex(k+1)< 0
I see that you did not even put a space in there.
Maybe you are still mentally writing code in another language, and expect that all languages use the same syntax. They don't. :)
I would strongly suggest looking at the flags that mlint posts in the editor. They would have pointed out at least some of these errors. As well, write your code more carefully, more slowly. Read what you wrote. These are errors that one makes when they are not thinking about what they are doing. You gain here by spending less time debugging, for a small cost in time to write the code.
2 个评论
Stephen23
2015-8-10
编辑:Stephen23
2015-8-10
How to learn MATLAB:
If you write large blocks of untested code don't then be surprised when it does not do what you expect it to. MATLAB is not C++, so why should code be written in the same way? Check your work, check every calculation, check every intermediate and output value, check every step, check every function that you use. Use the documentation and think about what you are doing. Then you will learn how to write code in MATLAB.
Although you wrote that the functions are used "according to the Matlab documentation", you should check this again for the if / else operator.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!