write text in file txt

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

write text in file txt

Octave - General mailing list
hi,

look that:

A = 0  0  0  1710  16
B = 0  0  0  2870  16
B = 0  0  0  1650  16

A B and C is a string

i want write it in a file .txt

i code it:

save gnuu.txt  A  -append
save gnuu.txt  B  -append
save gnuu.txt  C  -append

it's not correct ...i see it:

5.00000000e+01 4.80000000e+01 4.80000000e+01 4.80000000e+01 3.20000000e+01
3.20000000e+01 4.80000000e+01 3.20000000e+01 3.20000000e+01 4.50000000e+01
4.90000000e+01 4.80000000e+01 3.20000000e+01 3.20000000e+01 5.00000000e+01
5.00000000e+01 5.10000000e+01 4.80000000e+01 3.20000000e+01 3.20000000e+01
5.30000000e+01 5.00000000e+01


i want see this output:

0  0  0  1710  16
0  0  0  2870  16
0  0  0  1650  16





--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: write text in file txt

nrjank
> i want see this output:
>
>
> 0  0  0  1710  16
> 0  0  0  2870  16
> 0  0  0  1650  16
>

Here is what I get:
octave:1> A = "0 0 0 1710 16"
A = 0 0 0 1710 16
octave:2> B = "0 0 0 2870 16"
B = 0 0 0 2870 16
octave:3> C = "0 0 0 1650 16"
C = 0 0 0 1650 16
octave:5> save gnuu.txt A -append
octave:6> save gnuu.txt B -append
octave:7> save gnuu.txt C -append

the text file contains:

--------------------------------------
# Created by Octave 5.1.0, Mon Jul 01 17:48:32 2019 GMT <unknown@hostname>
# name: A
# type: string
# elements: 1
# length: 13
0 0 0 1710 16

# name: B
# type: string
# elements: 1
# length: 13
0 0 0 2870 16

# name: C
# type: string
# elements: 1
# length: 13
0 0 0 1650 16

--------------------------

the save command saves the variable information so that a load command can load them back into memory.  

it looks like you're using the -ascii option, which is saving the ascii charater values as a single precision matrix. I can recreate that by typing:

save -ascii "gnuu2.txt" A B C

---------------------------------
 4.80000000e+01 3.20000000e+01 4.80000000e+01 3.20000000e+01 4.80000000e+01 3.20000000e+01 4.90000000e+01 5.50000000e+01 4.90000000e+01 4.80000000e+01 3.20000000e+01 4.90000000e+01 5.40000000e+01
 4.80000000e+01 3.20000000e+01 4.80000000e+01 3.20000000e+01 4.80000000e+01 3.20000000e+01 5.00000000e+01 5.60000000e+01 5.50000000e+01 4.80000000e+01 3.20000000e+01 4.90000000e+01 5.40000000e+01
 4.80000000e+01 3.20000000e+01 4.80000000e+01 3.20000000e+01 4.80000000e+01 3.20000000e+01 4.90000000e+01 5.40000000e+01 5.30000000e+01 4.80000000e+01 3.20000000e+01 4.90000000e+01 5.40000000e+01
---------------------------------

to get your desired output, you can first convert your strings to numbers and then use the ascii output option:

>> D = str2num([A;B;C])
D =
      0      0      0   1710     16
      0      0      0   2870     16
      0      0      0   1650     16
octave:17> save -ascii "gnuu2.txt" D

---------------------------------
 0.00000000e+00 0.00000000e+00 0.00000000e+00 1.71000000e+03 1.60000000e+01
 0.00000000e+00 0.00000000e+00 0.00000000e+00 2.87000000e+03 1.60000000e+01
 0.00000000e+00 0.00000000e+00 0.00000000e+00 1.65000000e+03 1.60000000e+01
---------------------------------

if you want them written as integers, you might need to use some of the other file output commands. eg, 

> dlmwrite("gnuu3.txt",D," ")
------------------
0 0 0 1710 16
0 0 0 2870 16
0 0 0 1650 16
-------------------











Reply | Threaded
Open this post in threaded view
|

Re: write text in file txt

Doug Stewart-4
In reply to this post by Octave - General mailing list


On Mon, Jul 1, 2019 at 5:41 PM shivax via Help-octave <[hidden email]> wrote:
hi,

look that:

A = 0  0  0  1710  16
B = 0  0  0  2870  16
B = 0  0  0  1650  16

A B and C is a string

i want write it in a file .txt

i code it:

save gnuu.txt  A  -append
save gnuu.txt  B  -append
save gnuu.txt  C  -append

it's not correct ...i see it:

5.00000000e+01 4.80000000e+01 4.80000000e+01 4.80000000e+01 3.20000000e+01
3.20000000e+01 4.80000000e+01 3.20000000e+01 3.20000000e+01 4.50000000e+01
4.90000000e+01 4.80000000e+01 3.20000000e+01 3.20000000e+01 5.00000000e+01
5.00000000e+01 5.10000000e+01 4.80000000e+01 3.20000000e+01 3.20000000e+01
5.30000000e+01 5.00000000e+01


i want see this output:

0  0  0  1710  16
0  0  0  2870  16
0  0  0  1650  16





--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html



here is one way

A = "0  0  0  1710  16"
B = "0  0  0  2870  16"
C = "0  0  0  1650  16"


fid=fopen('doug.txt','w');
fdisp(fid,A)
fdisp(fid,B)
fdisp(fid,C)
fclose(fid)

--
DASCertificate for 206392



Reply | Threaded
Open this post in threaded view
|

Re: write text in file txt

Octave - General mailing list
it's okey!

thank you very much



--
Sent from: http://octave.1599824.n4.nabble.com/Octave-General-f1599825.html


Reply | Threaded
Open this post in threaded view
|

Re: write text in file txt

nrjank
If you need to do regular file input/output, I recommend reading the following sections of the manual to familiarize yourself with the simple and c-style file I/o functions: