tab delimited .txt file
조회 수: 2 (최근 30일)
이전 댓글 표시
I'm attempting to load data from excel into matlab, then alter it a bit and then load it into a .txt file. The code is below:
clear all
load data
data=data(:,5:end);
starting=input('Start (yyyy-mm-dd HH:MM):','s');
ending=input('End (yyyy-mm-dd HH:MM):','s');
resolution=input('Measurements taken every (minutes):');
DateTime=datestr(datenum(starting,'yyyy-mm-dd HH:MM'):resolution/(60*24):...
datenum(ending,'yyyy-mm-dd HH:MM'),...
'yyyy-mm-dd HH:MM');
DateTime=cellstr(DateTime);
outfile = '/path/to/file/output.txt';
header = {'DateTime','temp1','temp2','temp3','temp4','temp5','temp6','temp7',...
'temp8','temp9','temp10','temp11','temp12'};
fid = fopen(outfile, 'w');
if fid == -1; error('Cannot open file: %s', outfile); end
fprintf(fid, '%10s\t', header{:});
fprintf(fid, '\n');
for ii = 1:size(data, 1)
fprintf(fid, '%s\t', DateTime{ii});
fprintf(fid, '%10.6g\t', data(ii,:));
fprintf(fid, '\n');
end
fclose(fid)
The only problem with the code is that I am using it in order to input data into a program which requires the data to be inserted as a tab delimited .txt file. I thought that this code would do this but the program will not run. The program will run if I save the data in excel and then save it from there in a tab delimited .txt file. Is there anything in the code which restricts the data from being saved in a tab delimited format? Or am I missing a key part of coding?
cheers
댓글 수: 1
Jan
2011년 11월 24일
About see "clear all" see: http://www.mathworks.com/matlabcentral/answers/16484-good-programming-practice#answer_22301
답변 (1개)
Walter Roberson
2011년 11월 24일
You are producing an extra tab at the end of each data line; that might be causing problems.
Quick fix:
for ii = 1:size(data, 1)
fprintf(fid, '%s\t', DateTime{ii});
fprintf(fid, '%10.6g\t', data(ii,1:end-1));
fprintf(fid, '%10.6g\n', data(ii,end));
end
참고 항목
카테고리
Help Center 및 File Exchange에서 JSON Format에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!