load txt files with columns of numbers and text

13 次查看(过去 30 天)
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

采纳的回答

Geoff Hayes
Geoff Hayes 2015-11-4
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 个评论
Geoff Hayes
Geoff Hayes 2015-11-6
Try using the type command as
for n=1:6;
filename = ['WN', int2str(n), '.txt'];
system(['type ' filename ' >> LACW.txt']);
end

请先登录,再进行评论。

更多回答(2 个)

TastyPastry
TastyPastry 2015-11-4
编辑:Stephen23 2015-11-5
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 个评论
Katerina F
Katerina F 2015-11-5
编辑:Geoff Hayes 2015-11-6
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

请先登录,再进行评论。


Peter Perkins
Peter Perkins 2015-11-5
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.

类别

Help CenterFile Exchange 中查找有关 Text Data Preparation 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by