tab delimited .txt file

5 次查看(过去 30 天)
ricco
ricco 2011-11-24
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
Jan 2011-11-24
About see "clear all" see: http://www.mathworks.com/matlabcentral/answers/16484-good-programming-practice#answer_22301

请先登录,再进行评论。

回答(1 个)

Walter Roberson
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
  1 个评论
ricco
ricco 2011-11-24
good shout, but the program is really stubborn and still won't read the data, again it does if I open it in excel and then save it from there as a .txt file.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Standard File Formats 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by