How to print long integers through Octave script in terminal?

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

How to print long integers through Octave script in terminal?

thanos1983
This post has NOT been accepted by the mailing list yet.
Dear all,

Thank you in advance for your time and effort reading and replying to my question.

I am a completely new user to Octave, I have really short experience with Matlab but since I decided to switch from Windows to Linux I also decided to work with Octave from now on.

Well my script is extremely simple, I am trying to open a file with long values more than 6 digits after the decimal position. Sample of file:

1 1311265297.356461882500 1311265298.038403332250 0.68194144975 1252 0
2 1311265297.482088267750 1311265298.157206893000 0.67511862525 358 0
3 1311265297.654854238000 1311265298.157523691750 0.50266945375 1218 0
4 1311265297.792128980250 1311265298.158528506750 0.3663995265 417 0
5 1311265298.221279680750 1311265298.276972890000 0.05569320925 722 0
.
.
.
etc

Well my script is provided under:

#! octave-interpreter-name -qf

clear all;
close all;
clc;

filename = "output.txt";
fmt = "%i %f %f";
column_2 = load ("-ascii",filename,fmt);
printf ("This is the the number of rows: %d of: %s.\n",rows(column_2),filename);
format long;
printf ("%f\n",column_2);
#print("polynom.eps", "-deps");

The output when I call from the terminal with the command:

octave test.m

it prints:

1311265297.356461
1311265297.482088
.
.
.
etc.

It only prints the first 6 digits after I want to print more than 6 like 15 digits which is the max of a long format float.

Well on matlab the syntax that I am using is:

test_1 = importdata('output.txt');
column_1 = test_1(:,5);

Which extracts the 5th column of the *.txt file.

So I am also wondering how can I do that on Octave, extract only the 5th column.

Again thank you for your time and effort.

Best Regards,
Thanos
Reply | Threaded
Open this post in threaded view
|

Re: How to print long integers through Octave script in terminal?

Sebastian Schöps
> Well my script is extremely simple, I am trying to open a file with long values more than 6 digits after the decimal position. Sample of file:
[...]

> It only prints the first 6 digits after I want to print more than 6 like 15 digits which is the max of a long format float.
>
> Well on matlab the syntax that I am using is:
>
> test_1 = importdata('output.txt');
> column_1 = test_1(:,5);
>
> Which extracts the 5th column of the *.txt file.
>
> So I am also wondering how can I do that on Octave, extract only the 5th column.

Dear Thanos,

it seems that you are confusing integers and floating point numbers. I guess that you want to print long *floats*? But be warned, it is not wise to care about the last digit of a very long floating point number. If you are not aware of this please read e.g. http://en.wikipedia.org/wiki/Floating_point and maybe http://en.wikipedia.org/wiki/Machine_epsilon before messing around with data.

To answer your question: I recommend to use "fprintf" as described at http://www.gnu.org/software/octave/doc/interpreter/Formatted-Output.html
to format output. This gives more control than using "format long".

The following works in Octave and Matlab and should help you to get started

%-----
% read tabular data from file
table = load ('output.txt','-ascii');
% loop over all columns and print
for col=1:size(table,2)
fprintf('Column %d\n',col);
fprintf('%.6f\n',table(:,col));
end
%-----

Bye
Seb.
Reply | Threaded
Open this post in threaded view
|

Re: How to print long integers through Octave script in terminal?

thanos1983
This post has NOT been accepted by the mailing list yet.
Dear Seb. ,

Thanks for your time and effort assisting me with my problem. Your solution works perfectly, I was just curious how to store different columns in different variables (e.g. x and y) so I could plot them. But I hope that I would be able to figure this out.

Again thanks for your time and effort.

Best Regards,
Thanos