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));
0 个评论
采纳的回答
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
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
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] = ismember({'S','O','S'}, alphanum)
M = join(morse(loc), ' ')
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!