1ãç¨csvreadå½æ°
注æï¼csvreadå½æ°åªè¯ç¨ä¸ç¨éå·åéç纯æ°åæ件
第ä¸ç§ï¼M = CSVREAD('FILENAME') ï¼ç´æ¥è¯»åcsvæ件çæ°æ®ï¼å¹¶è¿åç»M
第äºç§ï¼M = CSVREAD('FILENAME',R,C) ï¼è¯»åcsvæ件ä¸ä»ç¬¬R-1è¡ï¼ç¬¬C-1åçæ°æ®å¼å§çæ°æ®ï¼è¿å¯¹å¸¦æ头æ件说æçcsvæ件(å¦ç¤ºæ³¢å¨çééçæ件)ç读åæ¯å¾éè¦çã
第ä¸ç§ï¼M = CSVREAD('FILENAME',R,C,RNG)ï¼å
¶ä¸ RNG = [R1 C1 R2 C2]ï¼è¯»åå·¦ä¸è§ä¸ºç´¢å¼ä¸º(R1,C1) ,å³ä¸è§ç´¢å¼ä¸º(R2,C2)çç©éµä¸çæ°æ®ã
注æï¼matlab认为CSV第1è¡ç¬¬1åçåå
æ ¼åæ 为ï¼0,0ï¼
ç»å®ä¸ä¸ªcsvlist.csvæ件,å
¶å
容å¦ä¸
02, 04, 06, 08, 10, 12
03, 06, 09, 12, 15, 18
05, 10, 15, 20, 25, 30
07, 14, 21, 28, 35, 42
11, 22, 33, 44, 55, 66
ä¾1.1 读åæ´ä¸ªæ件
csvread('csvlist.csv')
ans =
2 4 6 8 10 12
3 6 9 12 15 18
5 10 15 20 25 30
7 14 21 28 35 42
11 22 33 44 55 66
ä¾1.2 读å第2è¡ä»¥ä¸ï¼ç¬¬0å以å³åºåçæ°æ®
m = csvread('csvlist.dat', 2, 0)
m =
5 10 15 20 25 30
7 14 21 28 35 42
11 22 33 44 55 66
ä¾1.3 读å第2è¡ä»¥ä¸ï¼ç¬¬0å以å³ï¼ç¬¬3è¡ä»¥ä¸ï¼ç¬¬3å以左åºåçæ°æ®
m = csvread('csvlist.dat', 2, 0, [2,0,3,3])
m =
5 10 15 20
7 14 21 28
2ã使ç¨textscanå½æ°
å¨ä½¿ç¨textscanå½æ°åå¿
é¡»ç¨fopenå½æ°æå¼CSVæ件ãtextscanå½æ°è¯»åçç»æä¼åå¨cellæ°ç»ä¸ã
è°ç¨æ ¼å¼
C = textscan(fid, 'format')
C = textscan(fid, 'format', N)
C = textscan(fid, 'format', param, value, ...)
C = textscan(fid, 'format', N, param, value, ...)
C = textscan(str, ...)
[C, position] = textscan(...)
å
³äºtextscanå½æ°çå
·ä½ç¨æ³è§help textscanã
ä¾2.1 读åå符串
str = '0.41 8.24 3.57 6.24 9.27';
C = textscan(str, '%3.1f %*1d');
textscan returns a 1-by-1 cell array C:
C{1} = [0.4; 8.2; 3.5; 6.2; 9.2]
ä¾2.2 读åä¸åç±»åçæ°æ®
scan1.datæ件å
容å¦ä¸
Sally Level1 12.34 45 1.23e10 inf NaN Yes
Joe Level2 23.54 60 9e19 -inf 0.001 No
Bill Level3 34.90 12 2e5 10 100 No
ç¨åºå¦ä¸
fid = fopen('scan1.dat');
C = textscan(fid, '%s %s �2 � %u %f %f %s');
fclose(fid);
è¿åå¼Cæ¯ä¸ä¸ª1Ã8çå
èæ°ç»ï¼å
¶å¼å¦ä¸
C{1} = {'Sally'; 'Joe'; 'Bill'} class cell
C{2} = {'Level1'; 'Level2'; 'Level3'} class cell
C{3} = [12.34; 23.54; 34.9] class single
C{4} = [45; 60; 12] class int8
C{5} = [4294967295; 4294967295; 200000] class uint32
C{6} = [Inf; -Inf; 10] class double
C{7} = [NaN; 0.001; 100] class double
C{8} = {'Yes'; 'No'; 'No'} class cell
注æï¼C{5}çå两项è¶
åºäºuint32æ°å¼èå´ï¼æ以åªç»uint32çæ°å¼ä¸é
ä¾2.3 å»é¤ä¸åå符串
%å»é¤scan1.datä¸å°2åçå符串
fid = fopen('scan1.dat');
C = textscan(fid, '%s Level%u8 �2 � %u %f %f %s');
fclose(fid);
è¿åä¸ä¸ª1Ã8çå
èæ°ç»ï¼å
¶ä¸
C{2} = [1; 2; 3] class uint8
ä¾2.4 åªè¯»ç¬¬ä¸å
fid = fopen('scan1.dat');
names = textscan(fid, '%s %*[^\n]');
fclose(fid);
è¿åä¸ä¸ª1Ã1çå
èæ°ç»
names{1} = {'Sally'; 'Joe'; 'Bill'}
ä¾å2.5æå®çåé符å空å¼çæ¢ç®
data.csvæ件å
容å¦ä¸
1, 2, 3, 4, , 6
7, 8, 9, , 11, 12
ç¨åºå¦ä¸
fid = fopen('data.csv');
C = textscan(fid, '%f %f %f %f %u32 %f', 'delimiter', ',', ...
'EmptyValue', -Inf);
fclose(fid);
è¿åä¸ä¸ª1Ã6çå
èæ°ç»
C{1} = [1; 7] class double
C{2} = [2; 8] class double
C{3} = [3; 9] class double
C{4} = [4; -Inf] class double (empty converted to -Inf)
C{5} = [0; 11] class uint32 (empty converted to 0)
C{6} = [6; 12] class double
ä¾2.6 CSVæ件ä¸å«æ空å¼å注é
data2.csvå
容å¦ä¸
abc, 2, NA, 3, 4
// Comment Here
def, na, 5, 6, 7
å离åºæ³¨éè¯å¥
fid = fopen('data2.csv');
C = textscan(fid, '%s %n %n %n %n', 'delimiter', ',', ...
'treatAsEmpty', {'NA', 'na'}, ...
'commentStyle', '//');
fclose(fid);
è¿å1Ã5çå
èæ°ç»
C{1} = {'abc'; 'def'}
C{2} = [2; NaN]
C{3} = [NaN; 5]
C{4} = [3; 6]
C{5} = [4; 7]
ä¾2.7 å¤çéå¤åé符
data3.csv å
容å¦ä¸
1,2,3,,4
5,6,7,,8
å°multipledelimsasoneåæ°çå¼èµä¸º1ï¼åé¤éå¤çåé符
fid = fopen('data3.csv');
C = textscan(fid, '%f %f %f %f', 'delimiter', ',', ...
'MultipleDelimsAsOne', 1);
fclose(fid);
è¿åä¸ä¸ª1Ã4çå
èæ°ç»
C{1} = [1; 5]
C{2} = [2; 6]
C{3} = [3; 7]
C{4} = [4; 8]
ä¾2.8 使ç¨collectoutputå¼å
³
grades.txtå
容å¦ä¸
Student_ID | Test1 | Test2 | Test3
1 91.5 89.2 A
2 88.0 67.8 B
3 76.3 78.1 C
4 96.4 81.2 D
collectoutputå¼å
³çé»è®¤å¼ä¸º0ï¼falseï¼å°CSVä¸çæ¯ä¸åè¿åå°Cellçä¸åä¸ãå¦æå°å
¶å¼è®¾ä¸º1ï¼trueï¼ï¼åä¼æç¸åæ°æ®ç±»åçåè¿åå°Cellçä¸åä¸ã
%é»è®¤ä¸å¼å¯collectoutput
fid = fopen('grades.txt');
% read column headers
C_text = textscan(fid, '%s', 4, 'delimiter', '|');
% read numeric data
C_data0 = textscan(fid, '%d %f %f %s')
%å¼å¯collectoutput
frewind(fid);
C_text = textscan(fid, '%s', 4, 'delimiter', '|');
C_data1 = textscan(fid, '%d %f %f %s', ...
'CollectOutput', 1)
fclose(fid);
使ç¨collectoutputåï¼IDæ为cellä¸çä¸åï¼Test1åtest2åèµ·æ¥æ为cellä¸çä¸åï¼test3æ为cellä¸çä¸å
C_data0 =
[4x1 int32] [4x1 double] [4x1 double] {4x1 cell}
C_data1 =
[4x1 int32] [4x2 double] {4x1 cell}
frewindçä½ç¨æ¯è®©åé¢çtextscanå½æ°ä½¿ç¨åé¢çfidï¼ä¸ä¸ªfidåªè½è®©ä¸ä¸ªtextscan读
ä¾2.9 使ç¨ç¼ºççæ§å¶å符
å¦æè¦è¯»çå符串ä¸å
å«ä¸äºæ§å¶å符ï¼
\b Backspace
\n Newline
\r Carriage return
\t Tab
\\ Backslash (\)
å¦æä½ çæ°æ®ä½¿ç¨ä¸åçæ§å¶å符ï¼å¨è°ç¨textscanæ¶è½ä½¿ç¨sprintfå½æ°æ¾å¼è½¬æ¢è¿äºæ§å¶å符ã
温馨提示:答案为网友推荐,仅供参考