Hi,
I have a matrix called data (3652500 x 3). But data matrix dont have the header (i.e. names).
I want to insert column headers as shown below and write the output to a text file inserting 4th column from another matrix called output (3652500 x 1). I need to insert the output matrix in the 4th column of data matrix with 3 spaces between 3rd columns and 4th column. (See the header and sample rows below)
DATE TMAX TMIN RAIN
3000001 -99.9 -99.9 0.6
3000002 -99.9 -99.9 0.5
How can I do it MATLAB.
Any help is appreciated.
Thanks.

 채택된 답변

Image Analyst
Image Analyst 2014년 5월 23일
편집: Image Analyst 2014년 5월 24일

0 개 추천

With fprintf() you can make it look anyway you want. Have you tried that function?
output = 99*rand(10,4);
output(:,1) = round(output(:,1))
filename = 'deleteme.txt';
fid = fopen(filename, 'wt');
if fid ~= -1
fprintf(fid, 'DATE TMAX TMIN RAIN\n');
for row = 1 : size(output, 1)
fprintf(fid, '%d %.1f %.1f %.1f\n', output(row, :));
end
fclose(fid);
end

댓글 수: 5

Damith
Damith 2014년 5월 23일
I haven't tried that.
Where should my data matrix go in your code.? I need to create a text file. What values will it fill for 1, 2, and 3 columns.?
Thanks.
Image Analyst
Image Analyst 2014년 5월 24일
I forgot to put the fid inside the fprintf(), and 'wt' inside fopen(). It's correct now.
Damith
Damith 2014년 5월 25일
편집: Damith 2014년 5월 25일
Hi,
Thanks a lot for your code. It's working now but one more problem is as shown in the sample output is that I need to right justify the output so that decimal points are aligned properly.
Can you please suggest me a method in your code.
3792227 -99.9 -99.9 1.1
3792228 -99.9 -99.9 13.9
3792229 -99.9 -99.9 0.1
3792230 -99.9 -99.9 12.4
I need my output to appear as
3792227 -99.9 -99.9 1.1
3792228 -99.9 -99.9 13.9
3792229 -99.9 -99.9 0.1
3792230 -99.9 -99.9 12.4
Many thanks again.
Image Analyst
Image Analyst 2014년 5월 25일
It's just like any format string that you're used to in most any other language that you've ever used. If you use %.1f, the field width varies according to the size of the number. If you put %6.1f, it will pad out to make it a fixed field width of 6 characters.
Damith
Damith 2014년 5월 26일
Thanks. I figured it out.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

질문:

2014년 5월 23일

댓글:

2014년 5월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by