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

3 ビュー (過去 30 日間)
Lai Ken Siang
Lai Ken Siang 2022 年 4 月 13 日
コメント済み: Steven Lord 2022 年 4 月 14 日
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 Center および File ExchangeData Type Identification についてさらに検索

タグ

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by