Search code examples
matlabprintfnotepad++fopen

fprintf issues in writing and then reading


I'm trying to export a 2D matrix containing nodes coordinates of an Anys mesh (*.node), in Matlab. In this regard, I'm simply using fopen and fprintf commands in Matlab. For example:

========================================

fid = fopen('c:\new_nodes.node', 'wt'); fprintf(fid,'%3d %10.4e %10.4e %19.4e\n',new_nodes'); % new_nodes : my 2D matrix

=======================================

The content of generated file is okay but I cannot read it in Ansys, and when I open this file (e.g., new_nodes.node) in Notepad++ and do "save as", it becomes readable!!! It would be great if you could help me to find out whats the problem the Matlab code and how can I solve this problem... Thanks in advance,


Solution

  • Probably an error in the line brakes.

    Open the file as w instead of wt

    When you add the t matlab adds a \r after your \n

    [edit @ 17:22]

    fid = fopen('new_nodes.node', 'wt');
    fprintf(fid,'%3d %10.4e %10.4e %19.4e\n',[pi 2.1 exp(1) 2.5]);
    fclose(fid);
    fid = fopen('new_nodes.node','r');
    B = fread(fid,inf,'*uint8');
    fclose(fid);
    

    Now B contains 56 values that sum to 2664. What do you get?

    If I save it ase new_nodes2.node using notepad++ I get exactly the same values for B.