How to write horizontally

5 views (last 30 days)
Mekala balaji on 12 Jan 2020
Answered: Walter Roberson on 12 Jan 2020
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 on 12 Jan 2020
fid = fopen('TheFileName.txt','wt');
fprint(fid, 'x') ;
fprintf(fid, '%6g',x);
fprintf(fid, '\n') ;
Then same for y. Then
fclose(fid) ;

KALYAN ACHARJYA on 12 Jan 2020
Edited: KALYAN ACHARJYA on 12 Jan 2020
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

Show 1 older comment
KALYAN ACHARJYA on 12 Jan 2020
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
Mekala balaji on 12 Jan 2020
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,
KALYAN ACHARJYA on 12 Jan 2020