How can I make this loop work?
Afficher commentaires plus anciens
I want to create a function that can translate text input to morse code. But I am suck in the loop. e.g.
>> morserOutputFile('AB')
c =
1
c =
2
morsecode =
.-
Here is the code
function morserOutputFile(text)
Dit = '.';
ssp = ' ';
Dah = '-';
% Defining Characters & Numbers
A = strcat(Dit,ssp,Dah);
B = strcat(Dah,ssp,Dit,ssp,Dit,ssp,Dit);
C = strcat(Dah,ssp,Dit,ssp,Dah,ssp,Dit);
D = strcat(Dah,ssp,Dit,ssp,Dit);
E = strcat(Dit);
F = strcat(Dit,ssp,Dit,ssp,Dah,ssp,Dit);
G = strcat(Dah,ssp,Dah,ssp,Dit);
H = strcat(Dit,ssp,Dit,ssp,Dit,ssp,Dit);
I = strcat(Dit,ssp,Dit);
J = strcat(Dit,ssp,Dah,ssp,Dah,ssp,Dah);
K = strcat(Dah,ssp,Dit,ssp,Dah);
L = strcat(Dit,ssp,Dah,ssp,Dit,ssp,Dit);
M = strcat(Dah,ssp,Dah);
N = strcat(Dah,ssp,Dit);
O = strcat(Dah,ssp,Dah,ssp,Dah);
P = strcat(Dit,ssp,Dah,ssp,Dah,ssp,Dit);
Q = strcat(Dah,ssp,Dah,ssp,Dit,ssp,Dah);
R = strcat(Dit,ssp,Dah,ssp,Dit);
S = strcat(Dit,ssp,Dit,ssp,Dit);
T = strcat(Dah);
U = strcat(Dit,ssp,Dit,ssp,Dah);
V = strcat(Dit,ssp,Dit,ssp,Dit,ssp,Dah);
W = strcat(Dit,ssp,Dah,ssp,Dah);
X = strcat(Dah,ssp,Dit,ssp,Dit,ssp,Dah);
Y = strcat(Dah,ssp,Dit,ssp,Dah,ssp,Dah);
Z = strcat(Dah,ssp,Dah,ssp,Dit,ssp,Dit);
period = strcat(Dit,ssp,Dah,ssp,Dit,ssp,Dah,ssp,Dit,ssp,Dah);
comma = strcat(Dah,ssp,Dah,ssp,Dit,ssp,Dit,ssp,Dah,ssp,Dah);
question = strcat(Dit,ssp,Dit,ssp,Dah,ssp,Dah,ssp,Dit,ssp,Dit);
slash_ = strcat(Dah,ssp,Dit,ssp,Dit,ssp,Dah,ssp,Dit);
n1 = strcat(Dit,ssp,Dah,ssp,Dah,ssp,Dah,ssp,Dah);
n2 = strcat(Dit,ssp,Dit,ssp,Dah,ssp,Dah,ssp,Dah);
n3 = strcat(Dit,ssp,Dit,ssp,Dit,ssp,Dah,ssp,Dah);
n4 = strcat(Dit,ssp,Dit,ssp,Dit,ssp,Dit,ssp,Dah);
n5 = strcat(Dit,ssp,Dit,ssp,Dit,ssp,Dit,ssp,Dit);
n6 = strcat(Dah,ssp,Dit,ssp,Dit,ssp,Dit,ssp,Dit);
n7 = strcat(Dah,ssp,Dah,ssp,Dit,ssp,Dit,ssp,Dit);
n8 = strcat(Dah,ssp,Dah,ssp,Dah,ssp,Dit,ssp,Dit);
n9 = strcat(Dah,ssp,Dah,ssp,Dah,ssp,Dah,ssp,Dit);
n0 = strcat(Dah,ssp,Dah,ssp,Dah,ssp,Dah,ssp,Dah);
text = upper(text);
vars ={'period','comma','question','slash_'};
morsecode='';
a = length(text);
i = length(text);
while a > 0
c=(i-a)+1
switch c
case text(c)=='A'
morsecode = strcat(morsecode,A);
case text(c)=='B'
morsecode = strcat(morsecode,B);
case text(c)=='C'
morsecode = strcat(morsecode,C);
case text(c)=='D'
morsecode = strcat(morsecode,D);
case text(c)=='E'
morsecode = strcat(morsecode,E);
case text(c)=='F'
morsecode = strcat(morsecode,F);
end
a = a-1;
end
display(morsecode);
Réponses (1)
Walter Roberson
le 18 Juil 2016
Use an ascending for loop instead of a descending while loop.
Your code could be much more efficient if you used a cell array.
targets = ['A' : 'Z', '.,?/', '0' : '9'];
morse_translations = {A, B, C, D, .... };
[tf, idx] = ismember(text(c), targets);
if tf
this_code = morse_translations{idx};
...
end
1 commentaire
KAI YIN CHAN
le 18 Juil 2016
Catégories
En savoir plus sur Data Import and Export dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!