Error 'Array indices must be positive integers or logical values.' matlab gui
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Lai Ken Siang
el 13 de Abr. de 2022
Comentada: Steven Lord
el 14 de Abr. de 2022
I'm trying to convert morse code to texts and numbers. But it can only show one output for example, When I type '...' it shows 'S' however when I type '...---...'(morse code for SOS) there is an error 'Array indices must be positive integers or logical values.' and error in line:
set(handles.text11, 'string', alphanum(index));
This is my code:
input = char(get(handles.edit2,'string'));
morse = {'.----','..---','...--','....-','.....','-....','--...','---..',...
'----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....',...
'..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...',...
'-','..-','...-','.--','-..-','-.--','--..','/','.-.-.-'};
alphanum = {'1','2','3','4','5','6','7','8','9','0','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',' ','STOP'};
symbol = strsplit(input, ' ');
[~, index] = ismember(symbol, morse);
set(handles.text11, 'string', alphanum(index));
0 comentarios
Respuesta aceptada
Geoff Hayes
el 13 de Abr. de 2022
@Lai Ken Siang - I think that you will need to revisit your code. When the string is '...---...', how does the code know where one letter stops and the other ends. I think that you will need to add spaces between each character, then split the string on the space character. This will create an array of morse code signals. You will then need to iterate over each signal and convert that signal to the character (using the code above).
4 comentarios
Geoff Hayes
el 14 de Abr. de 2022
@Lai Ken Siang - I think instead you would need to have your input string look more like
'... --- ...'
where there are spaces between the Morse code signals. Then your other code would look more like
input = char(get(handles.edit2,'string'));
morse = {'.----','..---','...--','....-','.....','-....','--...','---..',...
'----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....',...
'..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...',...
'-','..-','...-','.--','-..-','-.--','--..','/','.-.-.-'};
alphanum = {'1','2','3','4','5','6','7','8','9','0','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',' ','STOP'};
symbols = strsplit(input, ' ');
morseToAlphaText = '';
for k=l:length(symbols)
signal = symbols{k};
[~, index] = ismember(signal, morse);
morseToAlphaText = [morseToAlphaText alphanum{index}];
end
set(handles.text11, 'string', morseToAlphaText);
I see that your code already had the strsplit which will return a cell array of strings. From that you just need to iterate over each element and convert it to the alpha numeric text. Remember that with cell arrays we use {} to index into the array rather than () so that we extract the data within the cell and not the cell itself.
Steven Lord
el 14 de Abr. de 2022
morse = {'.----','..---','...--','....-','.....','-....','--...','---..',...
'----.','-----','.-','-...','-.-.','-..','.','..-.','--.','....',...
'..','.---','-.-','.-..','--','-.','---','.--.','--.-','.-.','...',...
'-','..-','...-','.--','-..-','-.--','--..','/','.-.-.-'};
alphanum = {'1','2','3','4','5','6','7','8','9','0','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',' ','STOP'};
You can join together the Morse code segments for each letter. You need to transpose 'SOS' or split it into a cell array so ismember treats each character as its own entity rather than looking for the whole string 'SOS' in alphanum.
[~, loc] = ismember(transpose('SOS'), alphanum)
[~, loc] = ismember({'S','O','S'}, alphanum)
M = join(morse(loc), ' ')
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrix Indexing 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!