load txt files with columns of numbers and text

조회 수: 18 (최근 30일)
Katerina F
Katerina F 2015년 11월 4일
댓글: Katerina F 2015년 11월 8일
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
Katerina F
Katerina F 2015년 11월 8일
Thank you Geoff, This works fine. K.

댓글을 달려면 로그인하십시오.

추가 답변 (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 Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by