When execute the below code ? the error message appear 'Index exceeds the number of array elements'?
Mostrar comentarios más antiguos
M_h="FFFFFFFFFFFFFFFF";
M="";
for i=1:16
M(end+1:end+4)=dec2bin(hex2dec(M_h(i)), 4);
end
Respuesta aceptada
Más respuestas (1)
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
2 comentarios
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?
Walter Roberson
el 21 de Sept. de 2022
It was documented, at least at the time of introduction. I am having difficulty finding reference to it now.
Categorías
Más información sobre Characters and Strings en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!