Clear Filters
Clear Filters

Read txt file into a matrix

192 views (last 30 days)
Hans Jakob
Hans Jakob on 14 May 2015
Commented: Arman Jahedi on 19 Dec 2022
Hi, I have attached a txt file which I want to read onto a matrix
I also have another file, which has six columns instead of two. I want to skip the first two lines with text and blank, and read the rest into a matrix A. I have tried a lot of different things, but nothing seems to work for me. Hope somebody can help.
Best Regards Jakob

Accepted Answer

Stephen23
Stephen23 on 14 May 2015
Edited: Stephen23 on 14 May 2015
Just use textscan, it only requires a few lines of code:
fid = fopen('counts_VS_disp_2.txt','rt');
C = textscan(fid, '%f%f%f', 'MultipleDelimsAsOne',true, 'Delimiter','[;', 'HeaderLines',2);
fclose(fid);
And we can view the output cell array in the command window:
>> C
C =
[6779x1 double] [6779x1 double] [6779x1 double]
>> C{2}(1:10)
ans =
0
0.0090
0.0180
0.0270
0.0350
0.0440
0.0530
0.0620
0.0710
0.0800
For the six-column file, if the format is otherwise the same, you just need to change the format string from '%f%f%f' to '%f%f%f%f%f%f', and it should work.
And of course if you want all of those values in one numeric array rather than a cell array, just use cell2mat:
>> A = cell2mat(C);

More Answers (3)

Andy
Andy on 14 May 2015
Try this, auto-generated by using the Import Data tool and the Generate Script option in the drop down menu of "Import Selection"
%%Import data from text file.
% Script for importing data from the following text file:
%
% C:\Data\counts_VS_disp_2.txt
%
% To extend the code to different selected data or a different text file,
% generate a function instead of a script.
% Auto-generated by MATLAB on 2015/05/14 14:42:19
%%Initialize variables.
filename = 'C:\Data\counts_VS_disp_2.txt';
startRow = 2;
%%Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%6s%9s%s%[^\n\r]';
%%Open the text file.
fileID = fopen(filename,'r');
%%Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', '', 'WhiteSpace', '', 'HeaderLines' ,startRow-1, 'ReturnOnError', false);
%%Close the text file.
fclose(fileID);
%%Convert the contents of columns containing numeric strings to numbers.
% Replace non-numeric strings with NaN.
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);
for col=1:length(dataArray)-1
raw(1:length(dataArray{col}),col) = dataArray{col};
end
numericData = NaN(size(dataArray{1},1),size(dataArray,2));
for col=[1,2,3]
% Converts strings in the input cell array to numbers. Replaced non-numeric
% strings with NaN.
rawData = dataArray{col};
for row=1:size(rawData, 1);
% Create a regular expression to detect and remove non-numeric prefixes and
% suffixes.
regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)';
try
result = regexp(rawData{row}, regexstr, 'names');
numbers = result.numbers;
% Detected commas in non-thousand locations.
invalidThousandsSeparator = false;
if any(numbers==',');
thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$';
if isempty(regexp(thousandsRegExp, ',', 'once'));
numbers = NaN;
invalidThousandsSeparator = true;
end
end
% Convert numeric strings to numbers.
if ~invalidThousandsSeparator;
numbers = textscan(strrep(numbers, ',', ''), '%f');
numericData(row, col) = numbers{1};
raw{row, col} = numbers{1};
end
catch me
end
end
end
%%Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),raw); % Find non-numeric cells
raw(R) = {NaN}; % Replace non-numeric cells
%%Create output variable
countsVSdisp2 = cell2mat(raw);
%%Clear temporary variables
clearvars filename startRow formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me R;
  1 Comment
Safi Ullah
Safi Ullah on 14 Apr 2020
wow, thank you. I never heard of this feature in matlab. thats realy amazing feature. love that

Sign in to comment.


dpb
dpb on 14 May 2015
>> type hans.txt
Sample Time Encoder 2 Pos
[ 0 0.000 0; 1 0.009 0; 2 0.018 0; ]
>> fid=fopen('hans.txt');
>> c=cell2mat(textscan(fid,'%f%f%f','headerlines',2,'whitespace',' [;','collectoutput',1))
c =
0 0 0
1.0000 0.0090 0
2.0000 0.0180 0
>> fid=fclose(fid);
BTW, the "trick" for format strings to minimize typing for repetitive fields is
fmt=repmat('%f',1,N);
where N is the number of columns/fields. N can, of course be dynamic. Beats counting manually. NB: above that the closing ']' was ignored counting on the internal quiet failure of the conversion to terminate the read. It could have been included in the 'whitespace' list as well if desired for completeness.
BTW, for future, posting a file with 4 or 5 lines is just as informative as to the issues involved and much simpler for respondents.

Ali Raza
Ali Raza on 5 Feb 2020
I have a .txt file which i am attaching to this queery.
I want a metrix that contain all number enteries from row 32 till end. But the metrix enteries should include the Number values which are above row 32 by name of first and seconde prize bond winner Numbers.
Morever the number enteries should not miss zeros and every 1*1 cell have 6 digit No.
For example.
Number 000223
It should [000223]
it should not [223]

Categories

Find more on Text Data Preparation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!