How can I read a text file and import a part of it into matlab.

8 ビュー (過去 30 日間)
Bibhu Das
Bibhu Das 2016 年 6 月 23 日
編集済み: rocketboyjka 2016 年 6 月 23 日
I have a text file with 13 columns and some 900 rows. But,column 9 to 12 are asterisk and 13th is again numeric.I want to import only the numerics from 1st row to 38th row and 1st column to 8th column.Please help.
  1 件のコメント
Stephen23
Stephen23 2016 年 6 月 23 日
@Bibhu Das: please edit your question and upload the file by clicking the paperclip button.

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

採用された回答

dpb
dpb 2016 年 6 月 23 日
編集済み: dpb 2016 年 6 月 23 日
fmt=[repmat('%f',1,8) %*[^\n]']; % 8 values, skip rest of line format string
fid=fopen('yourfilenamehere'); % open the file; return file handle
data=cell2mat(textscan(fid,fmt,38,'collectoutput',1)); % read; repeat format 38 times
fid=fclose(fid); % close file
% do whatever w/ data here...
NB: I went ahead and wrapped the textscan call in cell2mat to return a double array. I assumed delimiter one of the default; if something else you'll need to fixup to match.
See
doc textscan % for details, examples, etc., ...
Or, option B:
Use the data import tool from the User Interface that (I think) will let you select an area to import from the file...not sure how it will do with such a long file but presume it'll handle it.

その他の回答 (1 件)

rocketboyjka
rocketboyjka 2016 年 6 月 23 日
編集済み: rocketboyjka 2016 年 6 月 23 日
Take a look at the the textscan() function. Something like this should work:
numRows = 38; % Or however many you want
fileID = fopen('MyFile.txt'); % Open the file
myData = textscan(fileID,'%f %f %f %f %f %f %f %f %s %s %s %s %f',numRows); % Read the specified format
fclose(fileID); % close the file
myData = myData([1:8]); % get rid of the columns you don't want
myData = cell2mat(myData); % Convert the cell array to a matrix

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by