フィルターのクリア

Read colomn data from file

1 回表示 (過去 30 日間)
Mani Ahmadian
Mani Ahmadian 2014 年 11 月 8 日
編集済み: per isakson 2014 年 11 月 8 日
Hi, I have a Unicode file as 'b.txt', contains 39 lines of header and data in other lines. I want to remove header lines and then read all data lines. I'm using this code:
FormatStr = repmat('%f ',1,12);
FileId = fopen('b.txt');
DataCell = textscan(FileId, FormatStr, 'Delimiter', ' ', 'HeaderLines', 39, 'CollectOutput', 1);
fclose(FileId);
TheData = DataCell{1};
But at final, TheData is in bad form. I want to have each column of data in a separate column in my variable TheData.
Please help me to solve it.
Thanks, Mani

採用された回答

per isakson
per isakson 2014 年 11 月 8 日
編集済み: per isakson 2014 年 11 月 8 日
Try
TheData = cssm();
where
function TheData = cssm()
FormatStr = repmat( '%f', 1,12 );
FileId = fopen('b.txt');
DataCell = textscan(FileId, FormatStr ...
, 'HeaderLines', 39, 'CollectOutput', true );
fclose(FileId);
TheData = DataCell{1};
end
The problem with your code has something to do with the delimiter being "one space" or "one or many spaces". And no need (/better not) to include space in the format-string. Default takes care of it. See the documentation.
  1 件のコメント
Mani Ahmadian
Mani Ahmadian 2014 年 11 月 8 日
Dear Per isakson, Nice code. Thanks a lot.

サインインしてコメントする。

その他の回答 (1 件)

Orion
Orion 2014 年 11 月 8 日
Hi, try this
FileId = fopen('b.txt');
DataCell = textscan(FileId, '%.8f','Delimiter','\n', 'HeaderLines', 39);
fclose(FileId);
DataCell = (reshape(cell2mat(DataCell),12,12))';
  1 件のコメント
Mani Ahmadian
Mani Ahmadian 2014 年 11 月 8 日
Dear Orion, Thanks for your fast answer. I test your code, it works on my sample file, it contains 12 rows and 12 colomns. My data file has 12 colomns but the rows are unknown. How I can reach to rows number to change your code as bellow?
MyRows=?
DataCell = (reshape(cell2mat(DataCell), MyRows ,12))';
Have a nice time
Mani

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by