If you change the ‘morsecode2’ assignment and cast it to a char rather than string data type, it works:
morsecode2 = morsecode;
The string data type cast creates a (1x1) array, so in the second (‘t’) loop, the iteration limits are 1:1, so the loop is satisfied at the outset, and never iterates. Eliminating the string cast allows the ‘t’ loop to iterate through the characters in ‘morsecode3’.
Personally, I would prefer a 1 kHz tone rather than a 700 Hz tone. (A personal preference only.)
Be sure to contribute this to the File Exchange!
—————
An oldie but a goodie was the morspeak function that I originally downloaded from the File Exchange in the mid-1990s. (I didn’t write it, unfortumately.) I modified it as morspeakn to include numbers and some punctuation, typically used in CW communications. I’m attaching it here.
This call to it produces code at about 15 WPM (words per minute).
code = morspeakn('hi there 1 2 3', 0.05, 0.15);
sound(code)
I used it during long simulations, to tell me where the simulation was in its iterations, using the computer’s speakers. (I could go do something else while the simulation was running.) I had it configured so that it would first use my call to alert me, so I could then listen to the message.
73 !