Converting cell to array data with specific string

조회 수: 3 (최근 30일)
Publius
Publius 2022년 11월 16일
댓글: Steven Lord 2022년 11월 17일
Hi,
I am trying to convert data read from a file to a numeric array. The format that it comes in as from an importdata command is:
{'[2, 11, 5, 0] ' }
{'[1, 10, 6, 0] ' }
{'[9, 3, 6, 0] ' }
{'[8, 1, 4, 0] ' }
{'[2, 4, 7, 0] ' }
{'[1, 3, 6, 0] ' }
{'[2, 3, 5, 0] ' }
{'[1, 2, 4, 0] ' }
{'[1, 2, 3, 0] ' }
I want to convert this to a double array of size n by 4. I want to do so without loops, preferably in one line (combined with the importdata command) to keep my code relatively concise. I have tried string, but that spits out an odd answer.
The data file includes those brackets, so I guess if anyone knows how to exclude MATLAB reading those, readmatrix can work (right now, the first and last columns are NaN because of those if I use that command).
Thank you!
  댓글 수: 2
Cris LaPierre
Cris LaPierre 2022년 11월 16일
Please attach your file (or a representative example) to your post using the paperclip icon.
Publius
Publius 2022년 11월 16일
Attached. It's originally a .dat file, but I couldn't upload it, so I changed it to a .txt. Should be the same.

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

채택된 답변

Stephen23
Stephen23 2022년 11월 17일
The simplest approach:
M = readmatrix('test.txt', 'TrimNonNumeric',true)
M = 5073×4
648 698 4494 623 4180 4181 1406 1415 4179 4180 1422 1415 4178 4179 1429 1422 4177 4178 1429 1434 4177 1434 4123 1438 4134 1441 4123 1438 1441 1443 4134 4145 4176 4145 1443 1444 1456 4185 1371 1358

추가 답변 (2개)

Cris LaPierre
Cris LaPierre 2022년 11월 16일
편집: Cris LaPierre 2022년 11월 16일
There are many ways to do this. Here are two. The result of the 2nd option is a table. See this page on Accessing Data in Tables
file = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1195758/test.txt';
data1 = readmatrix(file,'delimiter',["[","]",","," "],'ConsecutiveDelimitersRule','join',...
'LeadingDelimitersRule','ignore')
data1 = 5073×4
648 698 4494 623 4180 4181 1406 1415 4179 4180 1422 1415 4178 4179 1429 1422 4177 4178 1429 1434 4177 1434 4123 1438 4134 1441 4123 1438 1441 1443 4134 4145 4176 4145 1443 1444 1456 4185 1371 1358
data2 = readtable(file,'format','%*c %f %f %f %f %*c')
data2 = 5073×4 table
Var1 Var2 Var3 Var4 ____ ____ ____ ____ 648 698 4494 623 4180 4181 1406 1415 4179 4180 1422 1415 4178 4179 1429 1422 4177 4178 1429 1434 4177 1434 4123 1438 4134 1441 4123 1438 1441 1443 4134 4145 4176 4145 1443 1444 1456 4185 1371 1358 1384 4185 1371 4183 1384 1395 4182 4183 1406 1395 4181 4182 1557 1555 4515 0 4474 1557 1551 0 4474 4515 1557 0
  댓글 수: 3
Cris LaPierre
Cris LaPierre 2022년 11월 17일
In that case, Stephen23's answer is better in my opinion
Publius
Publius 2022년 11월 17일
Agreed, thank you for pointing that out. It came in afterward.

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


Jan
Jan 2022년 11월 16일
편집: Jan 2022년 11월 16일
data = {'[2, 11, 5, 0] '; '[1, 10, 6, 0] '; '[9, 3, 6, 0] '};
value = str2num(sprintf('%s;', data{:}))
value = 3×4
2 11 5 0 1 10 6 0 9 3 6 0
Internally str2num is based on eval. Maybe this is mor secure:
data2 = erase(string(data), "[");
data2 = replace(data2, "]" | ",", " ");
s = strjoin(data2);
value = reshape(sscanf(s, '%g'), 4, []).'
value = 3×4
2 11 5 0 1 10 6 0 9 3 6 0
  댓글 수: 1
Steven Lord
Steven Lord 2022년 11월 17일
If you're using release R2022a or later you can specify Evaluation='restricted' to limit what expressions in the text to be converted are executed.
str2num('[1, 2; 3, 4; 5:6]', Evaluation='restricted')
ans = 3×2
1 2 3 4 5 6
To preemptively answer your next question no, I don't have a definitive list of what counts as "basic math expressions" for purposes of restricted evaluation. I suspect if you stick to the operators in the Arithmetic Operators, Relational Operators, and Logical Operators sections on this documentation page along with array creation characters like square brackets, comma, semicolon, and colon you're probably okay.

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

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by