When execute the below code ? the error message appear 'Index exceeds the number of array elements'?
조회 수: 2(최근 30일)
표시 이전 댓글
M_h="FFFFFFFFFFFFFFFF";
M="";
for i=1:16
M(end+1:end+4)=dec2bin(hex2dec(M_h(i)), 4);
end
댓글 수: 0
채택된 답변
Jérôme
2022년 9월 12일
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
추가 답변(1개)
Walter Roberson
2022년 9월 21일
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
Walter Roberson
2022년 9월 21일
It was documented, at least at the time of introduction. I am having difficulty finding reference to it now.
참고 항목
범주
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!