Converting txt file string to matrix.

조회 수: 1 (최근 30일)
Hannah Taylor
Hannah Taylor 2020년 3월 31일
댓글: Hannah Taylor 2020년 3월 31일
I have an unhelpful txt file containing a string that looks something like:
{co-ordinates 1.5 2.5 4.8 weighting 11.7}{co-ordinates 2.5 2.8 1.7 weighting 21.4}{co-ordinates 1.5 2.5 4.8 weighting 11.7} .... etc
I'm trying to create a matrix with 4 collums (x co-ord, y co-ord, z co-ord, weighting) from this string. The co-ordinated are separated by a single space, the co-ords and weighting is separated by a double space.
Thanks in advance.
  댓글 수: 2
KSSV
KSSV 2020년 3월 31일
Read about textscan.
Hannah Taylor
Hannah Taylor 2020년 3월 31일
Is this similar to sscanf? I've been looking into this but unsure how to go about the 'format spec' part to say e.g. 'read the number after 'co-ordiantes ''

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

채택된 답변

Stephen23
Stephen23 2020년 3월 31일
편집: Stephen23 2020년 3월 31일
>> str = '{co-ordinates 1.5 2.5 4.8 weighting 11.7}{co-ordinates 2.5 2.8 1.7 weighting 21.4}{co-ordinates 1.5 2.5 4.8 weighting 11.7}';
>> mat = sscanf(str,'{co-ordinates%f%f%f weighting%f}',[4,Inf]).'
mat =
1.5 2.5 4.8 11.7
2.5 2.8 1.7 21.4
1.5 2.5 4.8 11.7
  댓글 수: 1
Hannah Taylor
Hannah Taylor 2020년 3월 31일
Thanks that's really helpful! I was struggling to understand the format spec part of sscanf but this has really helped :) life saver!!!

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

추가 답변 (0개)

카테고리

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

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by