Converting cell to array data with specific string

1 vue (au cours des 30 derniers jours)
Publius
Publius le 16 Nov 2022
Commenté : Steven Lord le 17 Nov 2022
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 commentaires
Cris LaPierre
Cris LaPierre le 16 Nov 2022
Please attach your file (or a representative example) to your post using the paperclip icon.
Publius
Publius le 16 Nov 2022
Attached. It's originally a .dat file, but I couldn't upload it, so I changed it to a .txt. Should be the same.

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 17 Nov 2022
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

Plus de réponses (2)

Cris LaPierre
Cris LaPierre le 16 Nov 2022
Modifié(e) : Cris LaPierre le 16 Nov 2022
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 commentaires
Cris LaPierre
Cris LaPierre le 17 Nov 2022
In that case, Stephen23's answer is better in my opinion
Publius
Publius le 17 Nov 2022
Agreed, thank you for pointing that out. It came in afterward.

Connectez-vous pour commenter.


Jan
Jan le 16 Nov 2022
Modifié(e) : Jan le 16 Nov 2022
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 commentaire
Steven Lord
Steven Lord le 17 Nov 2022
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.

Connectez-vous pour commenter.

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by