Help on try-catch statements
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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?
0 comentarios
Respuestas (2)
Walter Roberson
el 27 de Sept. de 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 comentarios
Jason Ross
el 27 de Sept. de 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.
0 comentarios
Ver también
Categorías
Más información sobre Scope Variables and Generate Names en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!