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!