필터 지우기
필터 지우기

How do I convert a text file into a string matrix؟

조회 수: 1 (최근 30일)
soheila
soheila 2016년 8월 19일
댓글: soheila 2016년 8월 19일
I have a text file 'h.txt'.It is assumed as follows:
a dd gf jh ju h ghh frd ghyg hhh hgfd
Each doc gfd ffd f ffd gtr asd ds das fdr fef ftf hg
f fdd gtf rrr ed
I want the output matrix is as follows:
  • I have one row for each line and each word is in a column.
  • For example, here There are three rows.
  • But the number of columns is different for each row(First row 11 column,in the other words a column for each word )

답변 (1개)

mbonus
mbonus 2016년 8월 19일
편집: mbonus 2016년 8월 19일
Try to import on line at a time in a for loop. Then within that loop convert the line into an array. You would then save that array to an index of a cell array. then at the end you can either keep the cell array, or you can find the longest row and pre-allocate a matrix of that size, then copy the data in the cell array over to the matrix, leaving the rest of the indexes as empty strings. The for loop would look like this:
cell_array = {};
for i = 1:numlinesinfile
current_line = import current line in file
cell_array{i} = current_line
end
  댓글 수: 1
soheila
soheila 2016년 8월 19일
Thank you. If you could explain in more detail. I've recently started working with MATLAB.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by