How can I replace a specific number (row/column) in .dat file
조회 수: 4 (최근 30일)
이전 댓글 표시
I need to use a .dat as an input, so I would like to replace specific row/column element (number) for each iteration. This element is located in the first column in which are numbers and letters (depending on the row), the other columns have only letters.
Do I need to change the file format to another one (.txt for instance), then make the replacement, and finally convert it to .dat again? Which options do I have?
I tried dlmwrite, fwrite and many others but unsuccessfully.
Thank you very much.
Alex
댓글 수: 0
답변 (1개)
Laurent
2014년 7월 10일
It depends on how your data is stored in the .dat file.
If it is ASCII, you can load it using
mydata=load(Filename,'-ascii');
Then you can do your replacements and save it using the save command.
댓글 수: 3
Laurent
2014년 7월 10일
OK, in that case you can do something like this (I didn't run it so there could be some small errors and I don't know if it is the fastest solution though):
%open file
fid=fopen(filename);
%go to beginning of the file
fseek(fid,0,-1);
data=[];
while feof(fid)==0 %do it for the complete file
thisline=fgetl(fid); % get the line of text
temp=textscan(thisline, '%f %f %f %f');
% reads for example 4 columns of floating point numbers
if ~isempty(temp{1})
data=[data; temp];
end
end
fclose(fid);
참고 항목
카테고리
Help Center 및 File Exchange에서 Printing and Saving에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!