Help on try-catch statements

1 view (last 30 days)
Jan Donyada
Jan Donyada on 27 Sep 2011
A section of my text-to-speech program is supposed to separate a Malay word into its prefix-base-suffix components. A word can exist in 4 forms; 1. Base, 2. Prefix-Base, 3. Base-Suffix, or 4. Prefix-Base-Suffix. Case 1 (Base) example: "Hapus". Case 2 (Prefix-Base) example: "Menghapus". Case 3 (Base-Suffix)example: "Hapuskan". Case 4 example: "Menghapuskan". This is my code so far:
try
[y, fs] = wavread(filename);
sound(y, fs);
catch
no_match = wordsstring{m};
x=4;
prefix = no_match(1:x);
prefix_filename = [wavdirectory prefix '.wav'];
if (exist(prefix_filename, 'file') == 0)
x=x-1;
if x==1 %this is where i need the case 3 (base-suffix) line of coding to kick in
end
else
[y, fs] = wavread(prefix_filename);
sound(y, fs);
remainder = no_match(x+1:end); %the remaining word can either be a base word (case 2 prefix-base) or base-suffix (case 4 prefix-base-suffix)
remaining_word = [wavdirectory remainder '.wav'];
if (exist(remaining_word, 'file') == 0) %if there is no match for the remaining word, then it must have a suffix.
z=3; %suffixes only go up to 3 letters in Malay
suffix = remainder(end-z,end); %scan last 3 characters
suffix_filename = [wavdirectory suffix '.wav'];
if (exist(suffix_filename, 'file') ==0) %if there is no matching suffix, decrement z
z=z-1;
if x==0
end
else
[y, fs] = wavread(suffix_filename);
sound(y, fs);
end
else
[y, fs] = wavread(remaining_word);
sound(y, fs);
end
end
end
The above code assumes that the word is case 4 (prefix-base-suffix) only, and does not take into account case 2 (prefix-base) and case 3 (base-suffix). Basically, first step is to check for prefixes (Case 2 or Case 4). If no prefixes are found, end the loop. Proceed to check for suffixes (case 3 only). How do I do this?
update ok I've updated the code, now it uses more exist() to simplify things. but the problem still remains. Any ideas?

Answers (2)

Walter Roberson
Walter Roberson on 27 Sep 2011
I wouldn't do things that way. You can determine whether a particular file exists by using exists(filename,'file') . That avoids the need to "try" playing the file and "catching" the fact the file does not exist.
  2 Comments
Jan Donyada
Jan Donyada on 27 Sep 2011
Ok noted. In the prefix scan, there can be two outcomes: a prefix is found, or there is no prefix (if(g==1) end). If a prefix is found, then its quite easy. I can just check if the remainder of the word is a base word (case 2 prefix-base), and if not, scan for suffixes (case 4 prefix-base-suffix). The problem is if the prefix scan outcome turns up negative. Then i need the program to jump out of the loop and straight away scan the word for suffixes. The problem with the case 2 and case 4 line of coding is that it doesn't allow for case 3 to be checked. What do you propose?
Jan Donyada
Jan Donyada on 27 Sep 2011
hmm currently it still won't read the suffix. "menghapus" outpus "meng" "hapus" correctly but "menghapuskan" just outputs "meng"

Sign in to comment.


Jason Ross
Jason Ross on 27 Sep 2011
When I get into a coding situation like this, I generally step away from the keyboard entirely and draw the problem out on a sheet of paper or a whiteboard. You can use a formal flowchart if you like, but sometimes just rough boxes and arrows for the decisions can clarify things immensely. But I'm a very visual person, so this may be more effective for me than others.
I would also suggest that you pick more descriptive variable names than a, x, y, z. It would likely make your code more readable.
You might want to also look into alternate loop structures, like a "while", as you could very easily iterate over the word segments until you've fully deciphered the word and then move on when you've hit the end.
It might also be helpful to separate your logical decisions from the actual processing. This would allow you to more easily test your logic by sending in a few test cases to make sure the logic actually works as you intend without dealing with the sound samples. You already have four test cases defined.

Categories

Find more on Entering Commands 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!