Problem in using Fwrite

1 Ansicht (letzte 30 Tage)
mohammad ali haghnejat
mohammad ali haghnejat am 26 Jul. 2017
Bearbeitet: Walter Roberson am 26 Jul. 2017
Hello
I want to rewrite or write a specific value in a file by fprintf command.
there are 2 issues that i have problems, the problems illustrate by examples:
1)for example A=123456789
i want to frprintf write A as scientific format in file, what should i do?like this 123.456789e6
2)after write this to number to file, i want to have 3 space and write another value in that line.
forexample A=123456789 and b=987654321 as below:
in special byte offset of my file i want to have: 123.456789e6 [3space] 98.7654321e7
Thank you.

Antworten (3)

Guillaume
Guillaume am 26 Jul. 2017
'%e' is the flag for scientific format. The spaces, you just put them in the format string, so:
fprintf(fid, '%e %e', A, b)
Additionally, you can specify the minimum number of characters that the %e conversion should occupy and the number of decimals.
  4 Kommentare
Guillaume
Guillaume am 26 Jul. 2017
So, rather than telling us which function (not command) you want to use and finding out it's not actually what you need, why don't you tell us exactly what it is you are trying to do? Then we can tell you how to go about it.
mohammad ali haghnejat
mohammad ali haghnejat am 26 Jul. 2017
Bearbeitet: Walter Roberson am 26 Jul. 2017
Thank you.
i have bdf file (which is used by Msc.Nastran) the format is as shown below.
PBEAM 2 119.0 286.5833286.58330.0 428.6875
1:ID
19.0:cross section
286.5833:Ix
286.5833:Iy
0.0:Ixy
428.6875:j
these are properties of beam element, in mechanical.
i want to use matlab to update these value in each iteration but there is problem cross section, Ix, Iy, Ixy, J should occupied 8 bytes.
in every iteration these values change and the number of digits would be change too.
there is one more issue, it should be concern that Msc.nastran recognize value if and only if they have decimal point, there for i wrote code and if my number is 6 digits or less adds .0 to fill 8 bytes but this work just for 6 digit number or less.
there is problem when my number has more than 6 digits, i want to convert my number in scientific form and instated of adding zero ( because in this case i cant add .0) add space.
thank you.

Melden Sie sich an, um zu kommentieren.


Walter Roberson
Walter Roberson am 26 Jul. 2017
The only time you can safely use fwrite() to update a file is if it is a binary file and you are updating fixed-width fields. It is not possible with fwrite() to replace '123456789' with '123.456789e6' because the number of bytes is different.
The only operating system that MATLAB has ever worked on, which had operating system support for squeezing in additional bytes in the middle of a file, was VMS -- and for technical reasons, it was not possible for MATLAB to access that facility.
What you will need to do instead is read from the input file, copy the parts before where you want to change into an output file, read the old value to be changed from the input file, write the modified version to the output file, and then resume copying from input to output.

mohammad ali haghnejat
mohammad ali haghnejat am 26 Jul. 2017
Thank you. i have bdf file (which is used by Msc.Nastran) the format is as shown below. PBEAM 2 119.0 286.5833286.58330.0 428.6875
1:ID
19.0:cross section
286.5833:Ix
286.5833:Iy
0.0:Ixy
428.6875:j
these are properties of beam element, in mechanical.
i want to use matlab to update these value in each iteration but there is problem cross section, Ix, Iy, Ixy, J should occupied 8 bytes.
in every iteration these values change and the number of digits would be change too.
there is one more issue, it should be concern that Msc.nastran recognize value if and only if they have decimal point, there for i wrote code and if my number is 6 digits or less adds .0 to fill 8 bytes but this work just for 6 digit number or less.
there is problem when my number has more than 6 digits, i want to convert my number in scientific form and instated of adding zero ( because in this case i cant add .0) add space.
thank you.

Kategorien

Mehr zu Install Products finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by