textscan output into one cell

조회 수: 10 (최근 30일)
amen45
amen45 2015년 12월 17일
댓글: amen45 2015년 12월 18일
I'm reading in a text file that contains many sets of strings separated by line and then by tab. I need to use textscan or something similar because of variability in the txt files I'll be using. Currently, I'm using code like the below to separate each of the strings into their own cell array, and then haphazardly group them into one cell(my desired output). I'm wondering if there is a more efficient way to go about this, and if it's possible to do this without the second loop or even the second text scan. Thank you in advance for your help!
for i=1:length(data)
check=char(data{i});
row= textscan(check, '%s', 'Delimiter', ' ');
for j= 1:length(row{1})
a{i,j}=row{1}{j}
end
end
Example text format:
disorganizedstring
disorganized string string
stringtype1 stringtype2 stringtype3 stringtype4
stringtype1 stringtype2 stringtype3 stringtype4
stringtype1 stringtype2 stringtype3 stringtype4
stringtype1 stringtype2 stringtype3 stringtype4
stringtype1 stringtype2 stringtype3 stringtype4
Current output:
'stringtype1' 'stringtype2' 'stringtype3' 'stringtype4'
'stringtype1' 'stringtype2' 'stringtype3' 'stringtype4'
'stringtype1' 'stringtype2' 'stringtype3' 'stringtype4'
'stringtype1' 'stringtype2' 'stringtype3' 'stringtype4'
'stringtype1' 'stringtype2' 'stringtype3' 'stringtype4'

채택된 답변

Walter Roberson
Walter Roberson 2015년 12월 18일
filecontent = fileread('YourFile.txt');
content_by_line = regexp(filecontent, '\r?\n', 'split');
content_by_field = regexp( content_by_line(:), '\t', 'split');
max_fields = max( cellfun(@length, content_by_field) );
cellpad = repmat({{}}, 1, max_fields);
first_n_fields = @(C, n) C(1:n);
padded_content = cellfun(@(C) first_n_fields([C,cellpad], max_fields), content_by_field, 'Uniform', 0);
desired_output = vertcat(padded_content{:});
This reads the file and splits it into lines and then splits the lines into fields. Then it finds the line with the most fields, and constructs padding as long as that. It then goes through and pads each line and takes the first N outputs: in this way without having to test how many fields there were on the line, each line is padded out to the same length. Once you have the cell array of cell arrays that are all the same length, a simple change converts it to a 2D cell array.
  댓글 수: 1
amen45
amen45 2015년 12월 18일
Thank you so much! this was very helpful!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by