Error; Unbalanced or unexpected parenthesis or bracket.

9 views (last 30 days)
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)

Answers (1)

John D'Errico
John D'Errico on 8 Aug 2015
Edited: John D'Errico on 8 Aug 2015
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 Comments
ASDFFGHHJKL123
ASDFFGHHJKL123 on 8 Aug 2015
Edited: ASDFFGHHJKL123 on 8 Aug 2015
Ok, first of all thank you for pointing out about the flags in the editor, I did not notice them before. :) Though my problem is that I am a beginner in Matlab so their tips are not that helpful.
Secondly this function obtains a matrix which has positive and negative entries. Then the program is supposed to count the occurrences when subsequent numbers have the same sign. Basically the program should give me an array where each element is going to tell me the number of times the numbers of the same sign appeared in the list. Then I am going to take that list and plot a histogram etc. I hope this helps.
Thirdly I still don't understand what is wrong with my code e.g. for statements. I used those in C++ (I might be subconsciously trying to use that code) and according to the Matlab documentation I don't see what the problem is?
P.S. I should also point out that I have almost no formal experience in writing computer code, thus the code I am writing is almost certainly very crude.
P.S. II Since I see you are very experienced in Matlab, do you mind me asking what would be some good beginners material for learning the Language?
Stephen23
Stephen23 on 10 Aug 2015
Edited: Stephen23 on 10 Aug 2015
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.

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!