Help on try-catch statements

1 次查看(过去 30 天)
Jan Donyada
Jan Donyada 2011-9-27
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?

回答(2 个)

Walter Roberson
Walter Roberson 2011-9-27
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 个评论
Jan Donyada
Jan Donyada 2011-9-27
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 2011-9-27
hmm currently it still won't read the suffix. "menghapus" outpus "meng" "hapus" correctly but "menghapuskan" just outputs "meng"

请先登录,再进行评论。


Jason Ross
Jason Ross 2011-9-27
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.

类别

Help CenterFile Exchange 中查找有关 Scope Variables and Generate Names 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by