load txt files with columns of numbers and text

22 views (last 30 days)
Hello, I would like to load files in Matlab with data that contain columns with letters too as shown in the attached file. I use the code below but it cannot load the file because of the text in the 7th column. Could you please tell me which code to use? It is a sequence of txt files that I want to load (WN1,WN2 etc up to WN6).
for n=1:6;
filename = ['WN', int2str(n), '.txt'];
WINDATA=load(filename);
save LACW.txt WINDATA -ascii -append;
end
thanks, K

Accepted Answer

Geoff Hayes
Geoff Hayes on 4 Nov 2015
Katerina - what are you trying to do with the contents of each file? From your above code, it appears that you read each file and then append its contents to another file. If you wish to just concatenate the contents of all files together then perhaps consider using cat or copy depending upon your operating system. For example, you could do something like
for n=1:6;
filename = ['WN', int2str(n), '.txt'];
system(['cat ' filename ' >> LACW.txt']);
end
If you still would like to load the data into MATLAB, try using importdata or readtable.
  4 Comments
Geoff Hayes
Geoff Hayes on 6 Nov 2015
Try using the type command as
for n=1:6;
filename = ['WN', int2str(n), '.txt'];
system(['type ' filename ' >> LACW.txt']);
end

Sign in to comment.

More Answers (2)

TastyPastry
TastyPastry on 4 Nov 2015
Edited: Stephen23 on 5 Nov 2015
Try:
fid = fopen('myFile.txt');
data = textscan(fid,'%s%s%d%d%d%d%s%d%d%d%d%d%d%d');
fclose(fid);
This will read your data in as a cell array of varying data types, strings for the dates, times and alphanumeric columns, int32s in the numeric columns.
  1 Comment
Katerina F
Katerina F on 5 Nov 2015
Edited: Geoff Hayes on 6 Nov 2015
Thanks, but where/how do I fit this into the code that I have shown you? I have many files and I want to make them one and save it. I tried as shown below but does not work. Could you please explain?
for n=1:6;
filename = ['WN', int2str(n), '.txt'];
fid = fopen('filename.txt');
data = textscan(fid,'%s%s%d%d%d%d%s%d%d%d%d%d%d%d');
fclose(fid);
save LACW.txt data -ascii -append;
end

Sign in to comment.


Peter Perkins
Peter Perkins on 5 Nov 2015
The first hit for a google search for "matlab load text file" leads to, as Stephen says, readtable and importtool. This is what it might look like with readtable:
>> t = readtable('WN20113.txt','delimiter','tab','ReadVariableNames',false)
t =
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14
____________ _______ ____ ____ ____ ____ _____ ____ ____ ______ _____ _____ _____ _____
'01/03/2011' '00:00' 1.1 1.1 1 1.7 'ENE' 2.6 0.6 1026.5 0 8.8 9 10
'01/03/2011' '00:10' 1 1.1 0.9 0.9 'ENE' 1.7 1 1026.6 0 8.7 10 10
'01/03/2011' '00:20' 0.7 0.9 0.4 0.9 'ENE' 3.5 0.7 1026.7 0 8.6 9 10
'01/03/2011' '00:30' 0.4 0.5 0.3 1.7 'ENE' 4.3 -0.2 1026.7 0 8.5 9 10
'01/03/2011' '00:40' 0.4 0.6 0.3 3.5 'ENE' 4.3 -1.7 1026.7 0 8.4 10 10
[snip]
>> t.Var7 = categorical(t.Var7);
>> t.Var7 = removecats(t.Var7,'---');
>> t.Var1 = datetime(strcat(t.Var1,{' '},t.Var2),'InputFormat','dd/MM/yyyy HH:mm');
>> t.Var2 = []
t =
Var1 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14
____________________ ____ ____ ____ ____ ____ ____ ____ ______ _____ _____ _____ _____
01-Mar-2011 00:00:00 1.1 1.1 1 1.7 ENE 2.6 0.6 1026.5 0 8.8 9 10
01-Mar-2011 00:10:00 1 1.1 0.9 0.9 ENE 1.7 1 1026.6 0 8.7 10 10
01-Mar-2011 00:20:00 0.7 0.9 0.4 0.9 ENE 3.5 0.7 1026.7 0 8.6 9 10
01-Mar-2011 00:30:00 0.4 0.5 0.3 1.7 ENE 4.3 -0.2 1026.7 0 8.5 9 10
01-Mar-2011 00:40:00 0.4 0.6 0.3 3.5 ENE 4.3 -1.7 1026.7 0 8.4 10 10
[snip]
If you have multiple files, concatenate them together after reading them all. Probably it would also be convenient if the files had column headers, so you don't end up with variables in the table with generic names like Var1, etc.

Community Treasure Hunt

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

Start Hunting!