Convert a Matrix to Struct ?

조회 수: 1 (최근 30일)
Aseel H
Aseel H 2011년 9월 22일
I have a binary vector as [0 1 1 0 0 1 0 1]; I want convert it as one struct [01100101]; I want it to get the character thtat corresponding for it(I know that but I can not convert avector to struct).
thanks.

채택된 답변

Jan
Jan 2011년 9월 22일
Please type doc struct in the command window to learn more about structs. "[01100101]" is not a struct.
If you want the CHAR vector (also called "string"):
a = [0 1 1 0 0 1 0 1]
s = sprintf('%d', a)
  댓글 수: 1
Walter Roberson
Walter Roberson 2011년 9월 23일
If you want the char vector, use
s = char(a+'0');

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

추가 답변 (1개)

Grzegorz Knor
Grzegorz Knor 2011년 9월 22일
Why do you want to create a struct?
Maybe just remove the spaces?
a = [0 1 1 0 0 1 0 1];
b = regexprep(num2str(a),'\W','')
  댓글 수: 2
Jan
Jan 2011년 9월 22일
This would be faster: b = strrep(num2str(a), ' ', '')
But why insert the spaces by NUM2STR only to remove them immediately. You can look in the source of NUM2STR to find out, that it is calling SPRINTF, such that the overhead can be avoided by calling it directly.
Grzegorz Knor
Grzegorz Knor 2011년 9월 22일
I agree with you. It was the first thing that came to my mind :)

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

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by