Info
This question is closed. Reopen it to edit or answer.
Trouble importing text file as matrix
6 views (last 30 days)
Show older comments
Hello, I am having problems importing a text file. I need to combine multiple text files into one which I execute through
cd 'C:\Users\mrmal\OneDrive\Documents\Matlab Practice Data\F1'
system('copy R1.txt+R2.txt+R3.txt F1.txt')
R1.txt R2.txt and R3.txt each are a 3x8 tab delimited matrix with %comments on the top of each. So, F1.txt looks something like:
%ihjbkj ibkj k hbjn
%nkevr erv erv erv evsfvsd
%sljnv sjvnnsk vsjvs
1 8 11 8 8 1 5 2
2 7 3 1 1 1 1 2
2 1 4 7 8 9 0 0
%ihjbkj ibkj k hbjn
%nkevr erv erv erv evsfvsd
%sljnv sjvnnsk vsjvs
1 8 11 8 8 1 5 2
2 7 3 1 1 1 1 2
10 2 4 2 2 2 2 2
and so on.
I need to import a matrix 9x8 so that I can find the average of column 3. I have tried many import tools, and the closest I have gotten was importing the data as 72 cells (9x8) When I try cell2mat I get
"Error using fopen
First input must be a file name or a file identifier."
and
"Error in cell2mat (line 28)
fileID = fopen(filename,'r');'
Any help or suggestions is appreciated.
1 Comment
Answers (1)
dpb
on 18 Jun 2017
Edited: dpb
on 19 Jun 2017
There's not a prepackaged routine to read multiple groups of data; you'll have to "roll your own" solution here. I'll show a couple of routes -- the first without concatenating the files first, just run a directory scan over the names with a wildcard and iterate over that collection:
pth='C:\Users\mrmal\OneDrive\Documents\Matlab Practice Data\F1';
d=dir(fullfile(pth,'R*.txt');
data=[];
for i=1:length(d)
data=[data,textread(d(i).name,'','headerlines',3,'delimiter','\t')];
end
If more than 3 R?.txt files, limit the loop to the name(s) wanted. While normally it's not best practice to dynamically allocate arrays as the above does, for such a small data set it won't hurt and saves some other coding hassles. textread has been deprecated to "red-haired stepchild" status but it's still useful for such tasks as here because it returns a regular double array instead of cell and uses the filename directly instead of needing a file handle. dlmwrite may work but it's not documented to succeed on files with other than numeric only data. Generally if it's just header lines it does work but...
Alternatively, if you have concatenated a group of files, then textscan can be called repetitively on the open file...
fid=fopen(fullfile(pth,'F1.txt'),'r');
data=[];
while ~feof(fid)
data=[data;cell2mat(textscan(fid,'','headerlines',3,'delimiter','\t','collectoutput',1))];
end
fid=fclose(fid); clear fid
Warning, danger Will Robinson!!!! Air code; not tested, ... :)
ADDENDUM
Actually, with the '%' comment line style instead of just text headers, it's simpler and while either works, the concatenated file can be read in a single call...
pth='C:\Users\mrmal\OneDrive\Documents\Matlab Practice Data\F1';
data=textread(fullfile(pth,'F1.txt','','commentstyle','matlab','delimiter','\t')];
If use textscan then the syntax for comment is to provide the character, not the style keyword...
data=cell2mat(textscan(fid,'','commentstyle','%','delimiter','\t'));
and will work for either the concatenated or individual files.
0 Comments
This question is closed.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!