Error 'Array indices must be positive integers or logical values.' matlab gui

1 次查看(过去 30 天)
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));

采纳的回答

Geoff Hayes
Geoff Hayes 2022-4-13
@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 个评论
Geoff Hayes
Geoff Hayes 2022-4-14
@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
Steven Lord 2022-4-14
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 = 3×1
29 25 29
[~, loc] = ismember({'S','O','S'}, alphanum)
loc = 1×3
29 25 29
M = join(morse(loc), ' ')
M = 1×1 cell array
{'... --- ...'}

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

标签

产品


版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by