Textscan with multiple delimiters

조회 수: 54 (최근 30일)
Andy
Andy 2013년 10월 21일
편집: per isakson 2013년 10월 21일
I want to read a text file into a cell array so that i have each line of my file as a new row and each attribute in my file as a column on that row.
file = fopen('file.txt');
d = textscan(file,'%s %d %d %d ,'delimiter',',');
If i run the code above it gives me a 1 x n array. If i type d{1} i get the first character of every line. Similarly if i type d{2} i get the second attribute of every line. Instead of having 1 x the number of attributes i would like the number of lines x the number of attributes so i can easily. Retrieve the values i need.
I asked a similar question yesterday but that gave me 2 separate arrays where i want all of this to be stored in the same cell array.
I have also tried this as my delimiter but it tells me the Delimiter must be a string.
'delimiter',{',','\n'}
  댓글 수: 1
Cedric
Cedric 2013년 10월 21일
Could you attach your data file or part of it?

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

채택된 답변

Kelly Kearney
Kelly Kearney 2013년 10월 21일
You should be able to get what you want with just a bit of post-processing following your textscan line:
str = sprintf('one,1,2,3\ntwo,4,5,6\n');
d = textscan(str,'%s %d %d %d', 'delimiter',',');
d = [d{1} num2cell(cat(2, d{2:4}))];
  댓글 수: 1
Andy
Andy 2013년 10월 21일
I have managed to access the data in its current form now. It took some time to work out how to access the cell array but i can now just use d{1}(2) to access column 1, 2nd element for example.

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

추가 답변 (1개)

per isakson
per isakson 2013년 10월 21일
편집: per isakson 2013년 10월 21일
I'm not sure I understand your question, but:
textscan( '1,2;3:4', '%d%d%d%d', 'Delimiter', ',;:' )
ans =
[1] [2] [3] [4]
>>

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by