Required format for simulation

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

Required format for simulation

GK19
Hi, I was running this code ( don't get into the details of the code)

[fid, msg] = fopen ( 'file1.csv' , 'wt' );
assert (fid> = 3, msg)
fprintf (fid, '% s \ t% s \ t% s \ n' , 'Temperature' , 'Value_n' , 'Value_p' );
for
    for
        for
            ...
            fprintf (fid, '% 3.0f \ t% 1.1f \ t% 1.1f \ n' , temp, vnw, vpw);
        end
    end
end
fclose (fid);

The output of the code
Temperature  Value_n Value_p ,-40 0.2 0.1 20 0.1 0.4 120 0.5 4.5

where as i should get
Temperature, -40 20 120
Value_n, 0.2 0.1 0.5
Value_p, 0.1 0.4 4.5

How can i change to get the above output


Reply | Threaded
Open this post in threaded view
|

Re: Required format for simulation

nrjank
Administrator

fprintf (fid, '% s \ t% s \ t% s \ n' , 'Temperature' , 'Value_n' , 'Value_p' );
for
    for
        for
            ...
            fprintf (fid, '% 3.0f \ t% 1.1f \ t% 1.1f \ n' , temp, vnw, vpw);
        end
    end
end
fclose (fid);

The output of the code
Temperature  Value_n Value_p ,-40 0.2 0.1 20 0.1 0.4 120 0.5 4.5

where as i should get
Temperature, -40 20 120
Value_n, 0.2 0.1 0.5
Value_p, 0.1 0.4 4.5

How can i change to get the above output


So I see two issues:  (1) the order of your numerical data is not in a way that would produce that output you desire (2) you have an issue with linebreaks not appearing.  

After copy/pasting your code and creating what I can only assume to be equivalent inputs and loop controls, it appears the code is doing exactly what you told it to do.  I get the  following:

Temperature  Value_n  Value_p 
-40 0.2 0.1
20 0.1 0.4
120 0.5 4.5

Note that in your code above there were a lot of spaces in the formatting text between the % and \ and other characters i had to remove. I assume this was just from posting to the mailing list. the cleaned up lines were:

fprintf (fid, '%s \t%s \t%s \n' , 'Temperature' , 'Value_n' , 'Value_p' );

and 

fprintf (fid, '%3.0f \t%1.1f \t%1.1f \n' , temp, vnw, vpw);

Did you see the message in the other thread about line breaks? depending on how the filesystem is reading the output, you may need the Windows style \r\n instead of just \n.  (I'm on windows, though, and seem no be fine with \n)

Anyway, if you want the data in a different order, you'll need to output it in the order you want.  If you're writing line by line, you'll need to read in all of the temperature data, then write that line. then read in all of the vnw data, then write that line.  then read in all of the vpw data, then write that line.  

Since this isn't a complete example where we can verify what's happening in your code, more guessing:

you're reading in the data line by line, and each line has a single Temp, vnw, and vpw value.  In that case you're going to have to read in the entire file, retain all values of temp, vnw, and vpw (you can store them in an array), then after you've read in the file you can write the output one line at a time.