フィルターのクリア

tab delimited .txt file

1 回表示 (過去 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 Center および File ExchangeSpreadsheets についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by