我目前正在做一些需要专家帮助的事情。我是Matlab的新手。这是要求。我有一个大型数据文件(该文件可以是最适合需要的任何格式,例如csv或txt或xlsx文件并具有混合内容),我必须从中提取数据并将其写入文本文件特殊字符串或特定行表示第15行。我必须在for循环中运行它,以便从数据文件中提取的数据被写入我已经拥有的多个文本文件中。我开发了一个代码,现在提取数据并将其写入文本文件,但它从一开始就替换文本的内容。我想在指定的位置插入输出(输出有多行)。这是当前可用的代码。
fidr = fopen('file1.csv','r') ;
% open file for writing
fidw = fopen('file2.txt','w') ;
% while end of file has not been reached
while ( ~feof(fidr) )
% read line from reading file
str = fgets(fidr) ;
% % %write line to writing file
fwrite(fidw,str) ;
end
答案 0 :(得分:0)
如果您使用Matlab分析大数据CSV文件集,我建议您使用DataStore,并了解如何使用table
不幸的是,对于您的案例datastore
仅供阅读(据我所知),但您可以这样继续,让我们假设我们有csv:
Batting.csv:
ds = tabularTextDatastore('Batting.csv');
>> ds.ReadSize = 1
ds =
TabularTextDatastore with properties:
Files: {
'/Users/toni/Documents/MATLAB/Batting.csv'
}
FileEncoding: 'UTF-8'
ReadVariableNames: true
VariableNames: {'playerID', 'yearID', 'stint' ... and 19 more}
Text Format Properties:
NumHeaderLines: 0
Delimiter: ','
RowDelimiter: '\r\n'
TreatAsMissing: ''
MissingValue: NaN
Advanced Text Format Properties:
TextscanFormats: {'%q', '%f', '%f' ... and 19 more}
TextType: 'char'
ExponentCharacters: 'eEdD'
CommentStyle: ''
Whitespace: ' \b\t'
MultipleDelimitersAsOne: false
Properties that control the table returned by preview, read, readall:
SelectedVariableNames: {'playerID', 'yearID', 'stint' ... and 19 more}
SelectedFormats: {'%q', '%f', '%f' ... and 19 more}
ReadSize: 1 rows
您可以预览内容:
>> preview(ds)
ans =
playerID yearID stint teamID lgID G AB R H x2B x3B HR RBI SB CS BB SO IBB HBP SH SF GIDP
___________ ______ _____ ______ ____ __ ___ __ __ ___ ___ __ ___ __ __ __ __ ___ ___ ___ ___ ____
'abercda01' 1871 1 'TRO' 'NA' 1 4 0 0 0 0 0 0 0 0 0 0 NaN NaN NaN NaN NaN
'addybo01' 1871 1 'RC1' 'NA' 25 118 30 32 6 0 0 13 8 1 4 0 NaN NaN NaN NaN NaN
'allisar01' 1871 1 'CL1' 'NA' 29 137 28 40 4 5 0 19 3 1 2 5 NaN NaN NaN NaN NaN
'allisdo01' 1871 1 'WS3' 'NA' 27 133 28 44 10 2 2 27 1 1 0 2 NaN NaN NaN NaN NaN
'ansonca01' 1871 1 'RC1' 'NA' 25 120 29 39 11 3 0 16 6 2 2 1 NaN NaN NaN NaN NaN
'armstbo01' 1871 1 'FW1' 'NA' 12 49 9 11 2 1 0 5 0 1 0 1 NaN NaN NaN NaN NaN
'barkeal01' 1871 1 'RC1' 'NA' 1 4 0 1 0 0 0 2 0 0 1 0 NaN NaN NaN NaN NaN
'barnero01' 1871 1 'BS1' 'NA' 31 157 66 63 10 9 0 34 11
使用数据表:
>> reset(ds)
>> while hasdata(ds)
T = read(ds);
disp(T(strcmp(T.teamID,'RC1'),:));
end
playerID yearID stint teamID lgID G AB R H x2B x3B HR RBI SB CS BB SO IBB HBP SH SF GIDP
___________ ______ _____ ______ ____ __ ___ __ __ ___ ___ __ ___ __ __ __ __ ___ ___ ___ ___ ____
'addybo01' 1871 1 'RC1' 'NA' 25 118 30 32 6 0 0 13 8 1 4 0 NaN NaN NaN NaN NaN
'ansonca01' 1871 1 'RC1' 'NA' 25 120 29 39 11 3 0 16 6 2 2 1 NaN NaN NaN NaN NaN
'barkeal01' 1871 1 'RC1' 'NA' 1 4 0 1 0 0 0 2 0 0 1 0 NaN NaN NaN NaN NaN
'birdge01' 1871 1 'RC1' 'NA' 25 106 19 28 2 5 0 13 1 0 3 2 NaN NaN NaN NaN NaN
'fishech01' 1871 1 'RC1' 'NA' 25 123 24 28 3 3 1 22 1 2 3 1 NaN NaN NaN NaN NaN
'fulmech01' 1871 1 'RC1' 'NA' 16 63 11 17 1 3 0 3 0 0 5 1 NaN NaN NaN NaN NaN
'hamra01' 1871 1 'RC1' 'NA' 25 113 25 28 4 0 0 12 6 2 1 7 NaN NaN NaN NaN NaN
'hastisc01' 1871 1 'RC1' 'NA' 25 118 27 30 6 4 0 20 11 2 2 4 NaN NaN NaN NaN NaN
'mackde01' 1871 1 'RC1' 'NA' 25 122 34 30 7 1 0 17 12 0 8 7 NaN NaN NaN NaN NaN
'sagerpo01' 1871 1 'RC1' 'NA' 8 39 9 11 0 0 0 5 5 1 2 2 NaN NaN NaN NaN NaN
'stirega01' 1871 1 'RC1' 'NA' 25 110 23 30 4 6 2 24 3 0 7 5 NaN NaN NaN NaN NaN
最后是一种让你工作的肮脏方式:
>> reset(ds)
>> T = readall(ds);
>> writetable(T(strcmp(T.teamID,'RC1'),:),'mydata.txt');
获取名为mydata.txt的文件
有了这个内容:
playerID,yearID,stint,teamID,lgID,G,AB,R,H,x2B,x3B,HR,RBI,SB,CS,BB,SO,IBB,HBP,SH,SF,GIDP
addybo01,1871,1,RC1,NA,25,118,30,32,6,0,0,13,8,1,4,0,NaN,NaN,NaN,NaN,NaN
ansonca01,1871,1,RC1,NA,25,120,29,39,11,3,0,16,6,2,2,1,NaN,NaN,NaN,NaN,NaN
barkeal01,1871,1,RC1,NA,1,4,0,1,0,0,0,2,0,0,1,0,NaN,NaN,NaN,NaN,NaN
birdge01,1871,1,RC1,NA,25,106,19,28,2,5,0,13,1,0,3,2,NaN,NaN,NaN,NaN,NaN
fishech01,1871,1,RC1,NA,25,123,24,28,3,3,1,22,1,2,3,1,NaN,NaN,NaN,NaN,NaN
fulmech01,1871,1,RC1,NA,16,63,11,17,1,3,0,3,0,0,5,1,NaN,NaN,NaN,NaN,NaN
hamra01,1871,1,RC1,NA,25,113,25,28,4,0,0,12,6,2,1,7,NaN,NaN,NaN,NaN,NaN
hastisc01,1871,1,RC1,NA,25,118,27,30,6,4,0,20,11,2,2,4,NaN,NaN,NaN,NaN,NaN
mackde01,1871,1,RC1,NA,25,122,34,30,7,1,0,17,12,0,8,7,NaN,NaN,NaN,NaN,NaN
sagerpo01,1871,1,RC1,NA,8,39,9,11,0,0,0,5,5,1,2,2,NaN,NaN,NaN,NaN,NaN
stirega01,1871,1,RC1,NA,25,110,23,30,4,6,2,24,3,0,7,5,NaN,NaN,NaN,NaN,NaN