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 Center および File ExchangeData Import and Export についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by