Info

This question is closed. Reopen it to edit or answer.

Creare table in txt file

2 views (last 30 days)
Nicola
Nicola on 10 Jan 2014
Closed: MATLAB Answer Bot on 20 Aug 2021
Good morning,
i have a problem with this simple code(i'm very noob):
x=[1,2,3,4,5];
y=[6,7,8,9,10];
p=fopen('risultato.txt', 'w');
fprintf(p,'%d\n\t%d\n',x,y);
fclose(p);
It writes
1
2
3
4
5
6
7
8
9
10
I want it writes
1 6
2 7
3 8
4 9
5 10
I tried to use the function table or cell2table but it gives me double error.
Thanks for your help

Answers (2)

Friedrich
Friedrich on 10 Jan 2014
Hi,
try this:
p=fopen('risultato.txt', 'w');
fprintf(p,'%d\t%d\n',[x;y]);
fclose(p);

Nicola
Nicola on 10 Jan 2014
Edited: Nicola on 10 Jan 2014
Thanks so much it works very well, but if i use the same trick in this code it doesn't work.. Can you help me?
angolo_minimo=20; %in gradi
lat=32.54;%Withesands
cost=pi/180;%serve per trasformare gradi in radianti: le funzioni trigonometriche richiedono argomenti in radianti
ora_solare=[0:0.1:24.00];%vettore ora orologio,
risultato = fopen('risultato.txt', 'wt');
giorno = 173;% giorno, 1-366
%calcolo la declinazione del sole
delta=23.45*sin(cost*360*(284+giorno)/365);%in gradi
omega=(12-ora_solare).*15; % ora solare apparente espressa con un angolo (gradi)
a=cos(cost*lat)*cos(cost*delta).*cos(cost.*omega);
b=sin(cost*lat)*sin(cost*delta);
argomento=a+b;
beta=asin(argomento)./cost; %in gradi
Ivet=find(beta>=angolo_minimo);%calcolo solo l'intervallo per angoli >=angolo_minimo
num=length(Ivet);
angolo_utile=beta(Ivet(1):Ivet(num));
ora_solare_utile=ora_solare(Ivet(1):Ivet(num));
fprintf(risultato,'%6g\t %6g\n',ora_solare_utile,angolo_utile);
It writes
6.7
6.8
6.9
7
7.1
7.2
7.3
7.4
7.5
7.6
......
20.792
22.0141
23.2399
24.4693
25.7021
26.....
I want it writes
6.7 20.792
6.8 23.2399
6.9 24.4693
7 257021
.....
...is to shorten. The first column is the X and the second one the Y of the axis(infact i plot(ora_solare_utile, angolo_utile) and it works).
Thanks So much
  2 Comments
Friedrich
Friedrich on 10 Jan 2014
take a look at my example. I used [x;y] for printf command and not x,y. So use [ora_solare_utile;angolo_utile] or [ora_solare_utile,angolo_utile] depending on the dimension of the two vectors
Nicola
Nicola on 10 Jan 2014
SOLVED!
Ivet=find(beta>=angolo_minimo);%calcolo solo l'intervallo per angoli >=angolo_minimo num=length(Ivet); for i=1:num angolo_utile(i)=beta(Ivet(i)); ora_solare_utile(i)=ora_solare(Ivet(i)); end
for k=1:num fprintf(risultato,'%5g\t%5g\n',ora_solare_utile(k),angolo_utile(k)); end

Community Treasure Hunt

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

Start Hunting!