tab delimited .txt file

4 vues (au cours des 30 derniers jours)
ricco
ricco le 24 Nov 2011
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 commentaire
Jan
Jan le 24 Nov 2011
About see "clear all" see: http://www.mathworks.com/matlabcentral/answers/16484-good-programming-practice#answer_22301

Connectez-vous pour commenter.

Réponses (1)

Walter Roberson
Walter Roberson le 24 Nov 2011
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 commentaire
ricco
ricco le 24 Nov 2011
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.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Standard File Formats dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by