load txt files with columns of numbers and text
显示 更早的评论
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
采纳的回答
更多回答(2 个)
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
2015-11-5
编辑:Geoff Hayes
2015-11-6
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Workspace Variables and MAT Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!