Table column of strings to a matrix.

조회 수: 1 (최근 30일)
Daniel
Daniel 2022년 11월 14일
답변: Steven Lord 2022년 11월 14일
Hello,
i am beginer and i am struggling with matlab tables. I have a table column that contains strings with vectors inside
A=["[0 0 0 0 0 0 0 0]";"[0 0 0 0 0 0 0 0]";"[0 0 0 0 0 0 0 0]";"[1 1 1 1 1 1 1 1]"]
A = 4×1 string array
"[0 0 0 0 0 0 0 0]" "[0 0 0 0 0 0 0 0]" "[0 0 0 0 0 0 0 0]" "[1 1 1 1 1 1 1 1]"
I would like to have a matrix with the numbers inside, like
B=[0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0;0 0 0 0 0 0 0 0;1 1 1 1 1 1 1 1]
B = 4×8
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
How can i do this without using loops? Thank you in advance.
  댓글 수: 2
Stephen23
Stephen23 2022년 11월 14일
편집: Stephen23 2022년 11월 14일
"I have a table column that contains strings with vectors inside "
This looks like file importing is suboptimal. You can probably change the file importing to import numeric as numeric, which most likely would be simpler and more efficient than messing around with text like this.
Jan
Jan 2022년 11월 14일
"I have a table column that contains strings with vectors inside" - no, there is no table. This is a column vector of strings. If the strings are interpreted as Matlab, they would be vectors, but they are not "inside".

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

채택된 답변

Stephen23
Stephen23 2022년 11월 14일
A = ["[0 0 0 0 0 0 0 0]";"[0 0 0 0 0 0 0 0]";"[0 0 0 0 0 0 0 0]";"[1 1 1 1 1 1 1 1]"]
A = 4×1 string array
"[0 0 0 0 0 0 0 0]" "[0 0 0 0 0 0 0 0]" "[0 0 0 0 0 0 0 0]" "[1 1 1 1 1 1 1 1]"
M = cell2mat(cellfun(@str2num,A,'uni',0))
M = 4×8
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1

추가 답변 (3개)

RAGHUNATHRAJU DASHARATHA
RAGHUNATHRAJU DASHARATHA 2022년 11월 14일
As per my understanding you want to get a matrix from the string array
I will demonstarte it using the example below
A=["[0 0 0 0 0 0 0 0]";"[0 0 0 0 0 0 0 0]";"[0 0 0 0 0 0 0 0]";"[1 1 1 1 1 1 1 1]"]
A = 4×1 string array
"[0 0 0 0 0 0 0 0]" "[0 0 0 0 0 0 0 0]" "[0 0 0 0 0 0 0 0]" "[1 1 1 1 1 1 1 1]"
B =extract(A,digitsPattern)
B = 4×8 string array
"0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "0" "1" "1" "1" "1" "1" "1" "1" "1"
B=str2double(B)
B = 4×8
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
for more information go through the link

Steven Lord
Steven Lord 2022년 11월 14일
A=["[0 0 0 0 0 0 0 0]";"[0 0 0 0 0 0 0 0]";"[0 0 0 0 0 0 0 0]";"[1 1 1 1 1 1 1 1]"]
A = 4×1 string array
"[0 0 0 0 0 0 0 0]" "[0 0 0 0 0 0 0 0]" "[0 0 0 0 0 0 0 0]" "[1 1 1 1 1 1 1 1]"
A2 = erase(A, ["[", "]"])
A2 = 4×1 string array
"0 0 0 0 0 0 0 0" "0 0 0 0 0 0 0 0" "0 0 0 0 0 0 0 0" "1 1 1 1 1 1 1 1"
double(split(A2, ' '))
ans = 4×8
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1

Jan
Jan 2022년 11월 14일
편집: Jan 2022년 11월 14일
A = ["[0 0 0 0 0 0 0 0]";"[0 0 0 0 0 0 0 0]";"[0 0 0 0 0 0 0 0]";"[1 1 1 1 1 1 1 1]"];
AC = char(A);
AC(ismember(AC, '[] ')) = [];
AC = reshape(AC, numel(A), []);
B = AC - '0'
B = 4×8
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1
Alternative:
AC = char(extract(A, digitsPattern));
B = squeeze(AC) - '0'
B = 4×8
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by