Hi,
I want to write 'x' and 'y' data side by side in each loop.
I sue below code
x=-5:1:5
y=x.^2
fid=fopen('test.txt','w')
for i = 1:lenth(x)
fprintf(fid,'%d\t\%d', (x(i) y(i))
end
fclose(fid)mat
but I am stillnot getting it.
desired outpit:
X -5 -4 -3 -2 -1 0 1 2 3 4 5
Y 25 16 9 4 1 0 1 4 9 16 25

 채택된 답변

Walter Roberson
Walter Roberson 2020년 1월 12일

0 개 추천

fid = fopen('TheFileName.txt','wt');
fprint(fid, 'x') ;
fprintf(fid, '%6g',x);
fprintf(fid, '\n') ;
Then same for y. Then
fclose(fid) ;

추가 답변 (1개)

KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 1월 12일
편집: KALYAN ACHARJYA 2020년 1월 12일

0 개 추천

x=-5:1:5;
y=x.^2;
disp('desired output:');
x
y
More
x=-5:1:5;
y=x.^2;
disp('desired outpit');
fprintf('x');
disp(x);
fprintf('y');
disp(y);
Result:
desired outpit
x -5 -4 -3 -2 -1 0 1 2 3 4 5
y 25 16 9 4 1 0 1 4 9 16 25

댓글 수: 4

Mekala balaji
Mekala balaji 2020년 1월 12일
I want to write 2rows and 11 columns, but current code is giving 11 rows and 2 columns. how to modify
fprintf(fid,'%d\t\%d', (x(i) y(i)) to achive desired output.
KALYAN ACHARJYA
KALYAN ACHARJYA 2020년 1월 12일
편집: KALYAN ACHARJYA 2020년 1월 12일
x=-5:1:5;
y=x.^2;
disp('desired outpit');
fprintf('x');
disp(x);
fprintf('y');
disp(y);
Result: 2 Rows 11 Columns
desired outpit
x -5 -4 -3 -2 -1 0 1 2 3 4 5
y 25 16 9 4 1 0 1 4 9 16 25
x=-5:1:5;
y=x.^2;
disp('desired outpit');
fprintf('x');
disp(x);
fprintf('y');
disp(y);
but, how to write this to file,

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

카테고리

도움말 센터File Exchange에서 MATLAB에 대해 자세히 알아보기

제품

릴리스

R2018b

태그

질문:

2020년 1월 12일

답변:

2020년 1월 12일

Community Treasure Hunt

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

Start Hunting!

Translated by