Delimiting a text file like "Text to columns"
5 ビュー (過去 30 日間)
古いコメントを表示
Hello, I am trying to import data from a Text file (.txt). This data has a single column of mixed (numeric and string) data that is | (bar) delimited. My goal is to import this data and seperate each delimited portion into a seperate columns while maintaining the rows. Essentially, I am trying to do the Excel "Text to columns" with MATLAB. Thank you
0 件のコメント
採用された回答
Walter Roberson
2011 年 9 月 14 日
%open file
fid = fopen(YourFile,'rt');
%figure out how many columns are there
firstline = fgetl(fid);
ncol = 1 + sum(firstline == '|');
%reset to beginning of file
fseek(fid,0,0);
%read data
indata = textscan(fid,repmat('%s',1,ncol),'Delimiter','|','CollectOutput',1);
%close file
fclose(fid);
The result will be indata, a cell array of strings.
4 件のコメント
Walter Roberson
2011 年 9 月 15 日
indata{L,C} will be the string that was on line #L at column #C
For example, indata{3,7} is column 7 of line 3.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Text Files についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!