When execute the below code ? the error message appear 'Index exceeds the number of array elements'?

1 visualización (últimos 30 días)
M_h="FFFFFFFFFFFFFFFF";
M="";
for i=1:16
M(end+1:end+4)=dec2bin(hex2dec(M_h(i)), 4);
end
Warning: Hexadecimal numbers representing integers greater than or equal to flintmax might not be represented exactly as double-precision floating-point values.
Index exceeds the number of array elements. Index must not exceed 1.

Respuesta aceptada

Jérôme
Jérôme el 12 de Sept. de 2022
M_h and M are string arrays because you used "". Here, you want to work with char arrays using '' (See for example these links for details about them: https://www.mathworks.com/help/matlab/characters-and-strings.html, https://www.mathworks.com/help/matlab/ref/char.html, https://www.mathworks.com/help/matlab/ref/string.html).
If you use the following code, you will not have the error anymore:
M_h='FFFFFFFFFFFFFFFF';
M='';
for i=1:16
M(end+1:end+4)=dec2bin(hex2dec(M_h(i)), 4);
end
  3 comentarios
Jérôme
Jérôme el 19 de Sept. de 2022
In Text in String and Character Arrays, you can see that MATLAB shows how to access individual characters of a char array, but not for a string. Because strings are made to work at a higher level, not to work characters per characters, which is the perfect job for char arrays.
In your case, you can simply convert the string to a char array before your operation, and convert it back to a string after that:
M_h="FFFFFFFFFFFFFFFF";
M_h2 = convertStringsToChars(M_h);
M2='';
for i=1:16
M2(end+1:end+4)=dec2bin(hex2dec(M_h2(i)), 4);
end
M = convertCharsToStrings(M2);
Otherwise, if you really want to manipulate string directly, I imagine it's possible using the function extractBetween.

Iniciar sesión para comentar.

Más respuestas (1)

Walter Roberson
Walter Roberson el 21 de Sept. de 2022
Using extractBetween() as suggested by @Jérôme is the approved way to work with characters inside strings. But you can also go to a lower level... which will give you faster code.
Double-quotes like you had is used for a string array, so below I show looping over a string array and using {} to extract characters
M_h = ["FFFFFFFFFFFFFFFF"; "DEADBEAFDEADBEAF"];
M = strings(size(M_h));
for K = 1 : numel(M_h)
this_M_in = M_h{K};
this_M_out = blanks(64);
for i=1:16
this_M_out(i*4-3:i*4)=dec2bin(hex2dec(this_M_in(i)), 4);
end
M(K) = string(this_M_out);
end
M
M = 2×1 string array
"1111111111111111111111111111111111111111111111111111111111111111" "1101111010101101101111101010111111011110101011011011111010101111"
  2 comentarios
Jérôme
Jérôme el 21 de Sept. de 2022
Nice, I didn't know we could convert a string to a char array using {}.
Is this mentioned somewhere in the official MATLAB documentation?

Iniciar sesión para comentar.

Categorías

Más información sobre Characters and Strings 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!

Translated by