How can a 4 element array index pull stored string values?

조회 수: 4 (최근 30일)
Akana Juliet
Akana Juliet 2021년 6월 18일
댓글: Akana Juliet 2021년 6월 18일
Hi all, I am new to MATLAB and I'm a bit stuck trying to figure out this part.
Let's say I have a 4 element vector [1x4 double] stored called "fourElementVector" (I have a function that changes these numbers a bit, situationally)
I also have a 256x1 cell "HexData" that stores a bunch of hexadecimal strings (ranging from 16bit to 128bit)
I'm trying to make the fourElementVector be the index and pull the values stored in HexData and store them as a string like:
[0B287 0B287 0B287 0B287] (just for an example. I don't want them converted)
The 4vector will always have a stored index numbers ranging from 1 - 256, and I have 256 hex codes, so it matches.
What do you think? How might I achieve this?

채택된 답변

Scott MacKenzie
Scott MacKenzie 2021년 6월 18일
편집: Scott MacKenzie 2021년 6월 18일
I think this is more or less what you are after. The output is generated both as a cell array and as a string vector.
% create random 16-bit hex values (as strings)
h = '0123456789abcdef'
idx = randi([1 16], 256, 4);
hexDataChar = h(idx);
% convert to 256x1 cell array of hex values
hexData = cellstr(hexDataChar);
% four element vector of indices
idx = randi([1 256], 1, 4);
% get four hex values from cell array
result1 = hexData(idx)
result2 = string(result1)
Output:
result1 =
4×1 cell array
{'b349'}
{'9cb2'}
{'cf42'}
{'1205'}
result2 =
4×1 string array
"b349"
"9cb2"
"cf42"
"1205"

추가 답변 (1개)

the cyclist
the cyclist 2021년 6월 18일
Can you upload examples of your variables fourElementVector and HexData, in a MAT file? This would be helpful.
But it seems from your description that
HexData{fourElementVector}
might do what you intend?
  댓글 수: 1
Akana Juliet
Akana Juliet 2021년 6월 18일
Thank you so much! I think I got it. Next time I'll share a bit more though!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by