سؤال

To run a certain software I'm using .txt-input files which I need to manipulate with Matlab. I know how to do it, and I didn't expected problems. As it was not working I reduced my manipulation script to a minimum, so actually nothing is changed. Except some white spaces, and the other software seems to react very sensitive on that.

parts of my file look like that:

...
*CONTROL_TERMINATION
$#  endtim    endcyc     dtmin    endeng    endmas
  1.000000         0     0.000     0.000     0.000
*CONTROL_TIMESTEP
$#  dtinit    tssfac      isdo    tslimt     dt2ms      lctm     erode     ms1st
     0.000  0.900000         0     0.000 -1.000E-4         0         0         0
$#  dt2msf   dt2mslc     imscl
     0.000         0         0
...

I'm loading it to Matlab and directly save it again without changes:

% read original file
fid = fopen('filename.txt','r');
param = textscan(fid,'%s','delimiter','\n');
rows = param{1,1};
fclose(fid);

% overwrite to new file
fid = fopen('filename.txt','w');
fprintf(fid, '%s\r\n', rows{:});
fclose(fid);

The output file is lacking of the white spaces at the begin of every line, that seems to be the only difference of input and output file. (at least I hope so)

...
*CONTROL_TERMINATION
$#  endtim    endcyc     dtmin    endeng    endmas
1.000000         0     0.000     0.000     0.000
*CONTROL_TIMESTEP
$#  dtinit    tssfac      isdo    tslimt     dt2ms      lctm     erode     ms1st
0.000  0.900000         0     0.000 -1.000E-4         0         0         0
$#  dt2msf   dt2mslc     imscl
0.000         0         0
...

Though it seems weird to me, that this should be the reason - what can I change, that both files look 100% identical? The problem I'm having is that the white spaces have different lengths.

هل كانت مفيدة؟

المحلول

You can use the whitespace option in textscan, and setting it to an empty string.

param = textscan(fid,'%s','delimiter','\n','whitespace','');

By default, textscan does not include leading white-space characters in the processing of any data fields (doc center).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top