Matlab 在具有不同字段的现有混合数字和字符串文本文件上写入
我有一个混合的数字和字符串文本文件,不同的字段被[]分割。其部分内容如下:Matlab 在具有不同字段的现有混合数字和字符串文本文件上写入,matlab,file,text,overwrite,Matlab,File,Text,Overwrite,我有一个混合的数字和字符串文本文件,不同的字段被[]分割。其部分内容如下: [JUNCTIONS] ;ID Elev Demand Pattern 2 0 0 ; 9 0 0
[JUNCTIONS]
;ID Elev Demand Pattern
2 0 0 ;
9 0 0 ;
5 0 11 ;
6 0 20 ;
[RESERVOIRS]
;ID Head Pattern
1 5 ;
4 50 ;
[PIPES]
;ID Node1 Node2 Length Diameter Roughness MinorLoss Status
66 2 9 1000 250 100 0 Open ;
2 9 4 1000 150 100 0 Open ;
3 9 5 1000 150 100 0 Open ;
4 2 6 1000 150 100 0 Open ;
我想覆盖MATLAB中“需求”和“粗糙度”列下面的数值变量。
你能帮帮我吗。我阅读了有关Matlab的导入和导出命令的性能,但我找不到解决方案。这里有一个小脚本,它可以将所有数据读取到单元格数组中。每个单元格都是一列数据。正如Divakar所说,使数据垂直对齐,否则当其中一行与最后一行不一致时,textscan将停止
fid = fopen(file,'r');
while 1
line=fgetl(fid);
if ~strcmp(line,'')
if ~isempty(strfind(line,'['))
% //Let's check it
start = strfind(line,'[');
split = strfind(line,']');
category = line(start+1:split-1);
if strcmp(category,'PIPES')
break;
end
end
end
end
% //skip the header line
line=fgetl(fid);
% //So now we're on the right line, let's read in the table.
data = textscan(fid,'%d %d %d %d %d %d %d %s');
fclose(fid);
如果正在编辑,请确保数据垂直对齐。