creating an array from a txt file

6 ビュー (過去 30 日間)
C Mck
C Mck 2016 年 6 月 28 日
コメント済み: Guillaume 2016 年 6 月 29 日
sorry if this is a silly question, but I have just started to use matlab.
I am trying to create an array using the txt file 'ifng.txt' however I want to remove the first row as it is just headers, this is what I have so far
function output = IFNG2016
fid= fopen('ifng.txt','r');
A = textscan(fid, '%f', 'HeaderLines', 1)
however it just prints
A= [0x1 Double]
any assistance would be appreciated.
Thanks
  6 件のコメント
Star Strider
Star Strider 2016 年 6 月 28 日
@Andreas Donauer —
Post your Comment here as an Answer.
C Mck
C Mck 2016 年 6 月 28 日
@Andreas Donauer
thanks it works

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

採用された回答

Guillaume
Guillaume 2016 年 6 月 28 日
編集済み: Guillaume 2016 年 6 月 28 日
The reason your textscan does not work is simply because the first column (the probe name) is not made of numbers so the '%f' format fails on that. This would work (and is much better than the whole file parsing suggested by Andreas):
fid= fopen('ifng.txt','r');
A = textscan(fid, ['%s', repmat('%f', 1 , 25)], 'HeaderLines', 1);
Anumbers = [A{2:end}];
Note that in the header, your file uses a combination of tabs and spaces as the delimiter. If you actually fixed that, then reading the file would be as simple as:
t = readtable('ifng.txt', 'Delimiter', 'tab'); %or 'space' if you replace the tabs by spaces
As a bonus you get the header read properly as variable names, so you don't even need to skip it.
  4 件のコメント
C Mck
C Mck 2016 年 6 月 29 日
thanks, this works now, I was putting the missing ] i the wrong place. at the minute its is displaying 1.0e+05* and then the array, do you know how I can get it to display the full number as displayed in the txt file, I have tried 'format long' but that isnt working Thanks
Guillaume
Guillaume 2016 年 6 月 29 日
I tend to use format longg or format shortg, but that is completely unrelated to your original question (which I believe is solved, so you should accept the answer).

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by