我的程序应该读取一个文本文件并以摩尔斯电码输出。
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function morse(filename)
% MORSE converts text morse code and writes it to a file
% Defining Characters & Numbers
A = ['.-'];
B = ['-...'];
C = ['-.-.'];
D = ['-..'];
E = ['.'];
F = ['..-.'];
G = ['--.'];
H = ['....'];
I = ['..'];
J = ['.---'];
K = ['-.-'];
L = ['.-..'];
M = ['--'];
N = ['-.'];
O = ['---'];
P = ['.--.'];
Q = ['--.-'];
R = ['.-.'];
S = ['...'];
T = ['-'];
U = ['..-'];
V = ['...-'];
W = ['.--'];
X = ['-..-'];
Y = ['-.--'];
Z = ['--..'];
period = ['.-.-.-'];
comma = ['--..--'];
question = ['..--..'];
slash_ = ['-..-.'];
n1 = ['.----'];
n2 = ['..---'];
n3 = ['...--'];
n4 = ['....-'];
n5 = ['.....'];
n6 = ['-....'];
n7 = ['--...'];
n8 = ['---..'];
n9 = ['----.'];
n0 = ['-----'];
text = upper(text);
vars ={'period','comma','question','slash_'};
morsecode=[];
for i=1:length(text)
if isvarname(text(i))
morsecode = [morsecode;eval(text(i))];
elseif ismember(text(i),'.,?/')
x = findstr(text(i),'.,?/');
morsecode = [morsecode;eval(vars{x})];
elseif ~isempty(str2num(text(i)))
morsecode = [morsecode;eval(['n' text(i)])];
elseif text(i)==' '
morsecode = [' '];
end
morsecode = [morsecode;eval(text(i))];
end
code = morsecode;
if exist('file','var')
fprintf(code, '%c')
end