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

3 Ansichten (letzte 30 Tage)
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));

Akzeptierte Antwort

Geoff Hayes
Geoff Hayes am 13 Apr. 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 Kommentare
Geoff Hayes
Geoff Hayes am 14 Apr. 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
Steven Lord am 14 Apr. 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 = 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
{'... --- ...'}

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Type Identification finden Sie in Help Center und File Exchange

Tags

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by