FAQ
Hi, I am trying to transfer data I have in a weird format into go. It's a
large amount of data, so I'm trying to do it through a binary file. I have
a script to read the data into a matlab matrix, from matlab I construct a
binary file, and then I am trying to read it into go.

I can successfully read in the first 492 doubles using bufio.Read, but for
some reason the 492nd element only reads 6 bytes rather than the full
eight. I would like some help debugging this.

The relevant Matlab code is the following:

outVarnames = {'reynolds_stress_uu_xyz','reynolds_stress_vv_xyz',...
'reynolds_stress_uv_xyz', 'dx_mean_u_xyz', 'dy_mean_u_xyz',
'dx_mean_v_xyz',...
'dy_mean_v_xyz'};

% Save data in a binary format
fid = fopen('DNSData.bin','w');
fwrite(fid,'x_loc ');
fwrite(fid,'y_loc ');
for ii = 1:length(outVarnames)
fwrite(fid,outVarnames{ii});
fwrite(fid,' ');
end
fprintf(fid,'\n')

fwrite(fid, 1.0,'double',0,'l');
fwrite(fid, 17.3,'double',0,'l');
fwrite(fid, 5*10^60,'double',0,'l');
xLocs(1,100)
% % Print grid locations
for ii = 1:500
xLocs(ii)
fwrite(fid,xLocs(ii),'double',0,'l');
end

fclose(fid);

and the full Go script I am using can be found

http://play.golang.org/p/UJYPwmN5X6

If I run the Matlab loop from 490 to 500 (instead of 1 to 500), I can
successfully read in all of the elements, but if I run the loop from 490 to
1000, somewhere around the 490th entry again only 6 bytes are read rather
than 8. Does anyone have any ideas on why this would happen? If it's
possibly relevant, the xLocs variable is a matlab matrix that is 2304 by
385.

Thanks for your help, I'm stumped.

Brendan

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Dave Cheney at Jan 31, 2013 at 11:36 pm
    What is wrong with just doing

    binary.Read(b, binary.LittleEndian, &num)


    On Fri, Feb 1, 2013 at 10:30 AM, Brendan Tracey
    wrote:
    Hi, I am trying to transfer data I have in a weird format into go. It's a
    large amount of data, so I'm trying to do it through a binary file. I have a
    script to read the data into a matlab matrix, from matlab I construct a
    binary file, and then I am trying to read it into go.

    I can successfully read in the first 492 doubles using bufio.Read, but for
    some reason the 492nd element only reads 6 bytes rather than the full eight.
    I would like some help debugging this.

    The relevant Matlab code is the following:

    outVarnames = {'reynolds_stress_uu_xyz','reynolds_stress_vv_xyz',...
    'reynolds_stress_uv_xyz', 'dx_mean_u_xyz', 'dy_mean_u_xyz',
    'dx_mean_v_xyz',...
    'dy_mean_v_xyz'};

    % Save data in a binary format
    fid = fopen('DNSData.bin','w');
    fwrite(fid,'x_loc ');
    fwrite(fid,'y_loc ');
    for ii = 1:length(outVarnames)
    fwrite(fid,outVarnames{ii});
    fwrite(fid,' ');
    end
    fprintf(fid,'\n')

    fwrite(fid, 1.0,'double',0,'l');
    fwrite(fid, 17.3,'double',0,'l');
    fwrite(fid, 5*10^60,'double',0,'l');
    xLocs(1,100)
    % % Print grid locations
    for ii = 1:500
    xLocs(ii)
    fwrite(fid,xLocs(ii),'double',0,'l');
    end

    fclose(fid);

    and the full Go script I am using can be found

    http://play.golang.org/p/UJYPwmN5X6

    If I run the Matlab loop from 490 to 500 (instead of 1 to 500), I can
    successfully read in all of the elements, but if I run the loop from 490 to
    1000, somewhere around the 490th entry again only 6 bytes are read rather
    than 8. Does anyone have any ideas on why this would happen? If it's
    possibly relevant, the xLocs variable is a matlab matrix that is 2304 by
    385.

    Thanks for your help, I'm stumped.

    Brendan

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Brendan Tracey at Feb 1, 2013 at 12:40 am
    Well, I thought I had tried that, but apparently not. Works now, thank you.
    Also thanks for the pointer on defer
    On Thursday, January 31, 2013 3:36:09 PM UTC-8, Dave Cheney wrote:

    What is wrong with just doing

    binary.Read(b, binary.LittleEndian, &num)


    On Fri, Feb 1, 2013 at 10:30 AM, Brendan Tracey
    <tracey....@gmail.com <javascript:>> wrote:
    Hi, I am trying to transfer data I have in a weird format into go. It's a
    large amount of data, so I'm trying to do it through a binary file. I have a
    script to read the data into a matlab matrix, from matlab I construct a
    binary file, and then I am trying to read it into go.

    I can successfully read in the first 492 doubles using bufio.Read, but for
    some reason the 492nd element only reads 6 bytes rather than the full eight.
    I would like some help debugging this.

    The relevant Matlab code is the following:

    outVarnames = {'reynolds_stress_uu_xyz','reynolds_stress_vv_xyz',...
    'reynolds_stress_uv_xyz', 'dx_mean_u_xyz', 'dy_mean_u_xyz',
    'dx_mean_v_xyz',...
    'dy_mean_v_xyz'};

    % Save data in a binary format
    fid = fopen('DNSData.bin','w');
    fwrite(fid,'x_loc ');
    fwrite(fid,'y_loc ');
    for ii = 1:length(outVarnames)
    fwrite(fid,outVarnames{ii});
    fwrite(fid,' ');
    end
    fprintf(fid,'\n')

    fwrite(fid, 1.0,'double',0,'l');
    fwrite(fid, 17.3,'double',0,'l');
    fwrite(fid, 5*10^60,'double',0,'l');
    xLocs(1,100)
    % % Print grid locations
    for ii = 1:500
    xLocs(ii)
    fwrite(fid,xLocs(ii),'double',0,'l');
    end

    fclose(fid);

    and the full Go script I am using can be found

    http://play.golang.org/p/UJYPwmN5X6

    If I run the Matlab loop from 490 to 500 (instead of 1 to 500), I can
    successfully read in all of the elements, but if I run the loop from 490 to
    1000, somewhere around the 490th entry again only 6 bytes are read rather
    than 8. Does anyone have any ideas on why this would happen? If it's
    possibly relevant, the xLocs variable is a matlab matrix that is 2304 by
    385.

    Thanks for your help, I'm stumped.

    Brendan

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts...@googlegroups.com <javascript:>.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Brendan Tracey at Jan 31, 2013 at 11:43 pm
    Oh, sorry, I should add, I'm using Windows 7 through the command prompt
    with the "go run" command. I believe I am using go 1.0.3. The error value
    printed out is <nil> until it reaches the end of file.
    On Thursday, January 31, 2013 3:30:38 PM UTC-8, Brendan Tracey wrote:

    Hi, I am trying to transfer data I have in a weird format into go. It's a
    large amount of data, so I'm trying to do it through a binary file. I have
    a script to read the data into a matlab matrix, from matlab I construct a
    binary file, and then I am trying to read it into go.

    I can successfully read in the first 492 doubles using bufio.Read, but
    for some reason the 492nd element only reads 6 bytes rather than the full
    eight. I would like some help debugging this.

    The relevant Matlab code is the following:

    outVarnames = {'reynolds_stress_uu_xyz','reynolds_stress_vv_xyz',...
    'reynolds_stress_uv_xyz', 'dx_mean_u_xyz', 'dy_mean_u_xyz',
    'dx_mean_v_xyz',...
    'dy_mean_v_xyz'};

    % Save data in a binary format
    fid = fopen('DNSData.bin','w');
    fwrite(fid,'x_loc ');
    fwrite(fid,'y_loc ');
    for ii = 1:length(outVarnames)
    fwrite(fid,outVarnames{ii});
    fwrite(fid,' ');
    end
    fprintf(fid,'\n')

    fwrite(fid, 1.0,'double',0,'l');
    fwrite(fid, 17.3,'double',0,'l');
    fwrite(fid, 5*10^60,'double',0,'l');
    xLocs(1,100)
    % % Print grid locations
    for ii = 1:500
    xLocs(ii)
    fwrite(fid,xLocs(ii),'double',0,'l');
    end

    fclose(fid);

    and the full Go script I am using can be found

    http://play.golang.org/p/UJYPwmN5X6

    If I run the Matlab loop from 490 to 500 (instead of 1 to 500), I can
    successfully read in all of the elements, but if I run the loop from 490 to
    1000, somewhere around the 490th entry again only 6 bytes are read rather
    than 8. Does anyone have any ideas on why this would happen? If it's
    possibly relevant, the xLocs variable is a matlab matrix that is 2304 by
    385.

    Thanks for your help, I'm stumped.

    Brendan
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Brainman at Feb 1, 2013 at 12:28 am
    Also, your

    defer file.Close()

    in the wrong place. You do not know if os.Open returned you are file to
    close until you checked the error returned. Therefore, you should move that
    line after your error check.

    Alex

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 31, '13 at 11:30p
activeFeb 1, '13 at 12:40a
posts5
users3
websitegolang.org

People

Translate

site design / logo © 2021 Grokbase