f



Save text string from array in specific way

Hello all,

I have a very specific question, which I hope is possible to perform with Matlab.
I am calculating the position (XYZ) of certain particles, and need to print this information to a file. Although this sounds easy, the problem comes from the saving part.

The information that needs to be saved is a so called PDB file, which is used as input for a different program. This program reads specific characters on a line of text, see this example:

ATOM      1  H1  LYS     1      14.260   6.590  34.480  1.00  0.00
ATOM      2  H2  LYS     1      13.760   5.000  34.340  1.00  0.00
ATOM      3  N   LYS     1      14.090   5.850  33.800  1.00  0.00
ATOM      4  H3  LYS     1      14.920   5.560  33.270  1.00  0.00

In this case, the first 6 characters are specified for a name (in this case ATOM, with 2 empty spaces. Character 7 is a blank, while the next 4 characters are a counter equal to the number of lines of text. Character 12 and 13 again are blank, while character 14, 15, and 16 are used for an Atom type. This continues for the rest of the information.

What I would like to know is how I can replicate this information. I can work my way around by using spaces or horizontal tabs, but this lining breaks when for instance a single digit counts up to become a double digit (from 9 -> 10). In this case, the added digit will shift all numbers one position, while it should remove one space.

I know this might sound very confusing, I really hope it is somewhat clear. I'll try to clarify it by giving another example below:

ATOM      4  H3  LYS     1      14.920   5.560  33.270  1.00  0.00

In the above situation there are 6 spaces between "ATOM"  and "4", and this remains the same when 4 will count up to for example 11. This will result in the rest of the vector to shift one position to the right, essentially destroying the layout of my file. How can I keep this layout?
0
Mark
12/19/2016 6:07:03 PM
comp.soft-sys.matlab 211264 articles. 26 followers. lunamoonmoon (257) is leader. Post Follow

1 Replies
505 Views

Similar Articles

[PageSpeed] 45

On 12/19/2016 12:07 PM, Mark Kamps wrote:
> Hello all,
>
> I have a very specific question, which I hope is possible to perform
> with Matlab.
> I am calculating the position (XYZ) of certain particles, and need to
> print this information to a file. Although this sounds easy, the problem
> comes from the saving part.
>
> The information that needs to be saved is a so called PDB file, which is
> used as input for a different program. This program reads specific
> characters on a line of text, see this example:
>
> ATOM 1 H1 LYS 1 14.260 6.590 34.480 1.00 0.00
> ATOM 2 H2 LYS 1 13.760 5.000 34.340 1.00 0.00
> ATOM 3 N  LYS 1 14.090 5.850 33.800 1.00 0.00
> ATOM 4 H3 LYS 1 14.920 5.560 33.270 1.00 0.00
>
> In this case, the first 6 characters are specified for a name (in this
> case ATOM, with 2 empty spaces. Character 7 is a blank, while the next 4
> characters are a counter equal to the number of lines of text. Character
> 12 and 13 again are blank, while character 14, 15, and 16 are used for
> an Atom type. This continues for the rest of the information.
>
> What I would like to know is how I can replicate this information. ...

Just write the appropriate format

fmt=['%6s%5d%4s%2d' repmat('%7.3f',1,3) repmat('%5.2f',1,2)];

Then simply use FPRINTF to write the data from whatever variables 
contain the values..."piece o' cake!"  :)
0
dpb
12/19/2016 9:18:22 PM
Reply: