I need to create a text file with 3 columns. first two columns are of the same size, 100. The 3rd column just has one number in it. How can I do that?

 채택된 답변

Walter Roberson
Walter Roberson 2012년 2월 21일

1 개 추천

With col1, col2, and col3 being column vectors:
fmt = ['%f %f %f\n' repmat('%f %f\n', 1, 99)];
fid = fopen('YourOutputFile.txt', 'wt');
fprintf(fid, fmt, col1(1), col2(1), col3(1), [col1(2:end), col2(2:end)].');
fclose(fid);
Warning: the details of the fprintf() call depend upon col1 and col2 being column vectors!

댓글 수: 4

Baba
Baba 2012년 2월 21일
getting an error in 3rd line of your code
I created col3, as col3=[1]
set first two column vectors to col1 and col2
Baba
Baba 2012년 2월 21일
and the txt file is saved as blank
Walter Roberson
Walter Roberson 2012년 2월 21일
Sorry, dropped an important parameter. I have edited it in to the above.
Baba
Baba 2012년 2월 21일
Perfect thank you!

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

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2012년 2월 21일

0 개 추천

Do you want the third column to have 100 values the same?
If so:
A = [c1,c2,repmat(c3,100,1)];
If not:
A = [{c1} {c2} {c3}]
for more info:
doc cell

댓글 수: 3

Baba
Baba 2012년 2월 21일
yep, 3rd column should only have 1 value in it. I'm having trouble writing A to a txt file using a dlmwrite command.
Baba
Baba 2012년 2월 21일
that one value should not repeat, just appear in the first row.
Walter Roberson
Walter Roberson 2012년 2월 21일
In order to do it with dlmwrite(), you would have to make two dlmwrite() calls, with the second using the -append option.

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

카테고리

질문:

2012년 2월 21일

편집:

2013년 10월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by