creating an array from a txt file

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 个评论

It would be helpful to upload the file ifng.txt.
One of many posibilities:
% open the file
fid = fopen('ifng.txt'); % could check if fid is nonempty here
% read first line
tline = fgets(fid);
index = 0;
% if char (not end of file), keep reading
while ischar(tline)
index = index+1;
txt{index} = tline(1:end-1); % store line in a cell-array-of-strings
tline = fgets(fid);
end
% close file
fclose(fid);
% re-format starting from line 2
script = sprintf('%s',txt{2:end});
% replace line feeds (windows)
script(script==13) = sprintf('\n');
% finally, print what you've been reading in command line:
fprintf(script);
C Mck
C Mck 2016-6-28
编辑:C Mck 2016-6-28
i have edited the question and included the txt file
Have you tried what I posted? If this works for you, please check the question solved.
@Andreas Donauer —
Post your Comment here as an Answer.
@Andreas Donauer
thanks it works

请先登录,再进行评论。

 采纳的回答

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 个评论

this didn't work, when i try to print to the screen nothing aooears
As said, the readtable will only work if you fix the mixed tab/space issue in the file.
The other way works fine with the file you provided (after I fixed the missing ] that would have resulted in a syntax error if you tried it).
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
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 个)

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by