简体   繁体   中英

sprintf function: not working in octave the way it does for MATLAB

I need to call another commanline tool using octave .m files as wrapper. In following piece of code, it give eroor in line 'Detector'. It run quite nicely on Matlab but now I need to do it for octave. The documentation of Octave doesn't talk much about multi-line sprintf. Can anybody share solution to deal with such situations.

%s =sprintf(...
s =sscanf(...
[...
'                                                                   \n'...
'                                                                   \n'...
'Detector {                                                         \n'...    
'   [Plane: a_x(%f,%f,%f) a_y(%f,%f,%f) center(%f,%f,%f)]           \n'...
'   x1=%f  x2=%f  nx=%d                                             \n'...
'   y1=%f  y2=%f  ny=%d                                             \n'...
'   n_x_sub=%d n_y_sub=%d                                           \n'...        
                                                                                  \n'...
'   %s                                                              \n'...    
'}                                                                  \n'...
'                                                                   \n'...
Beam                                                            \n'...  
'   start(%f,%f,%f)                                                 \n'...
'   %s                                                              \n'...
'   %s                                                              \n'...
'   %s                                                              \n'...
'}                                                                  \n'...
'                                                                   \n'...    
'Commands {                                                         \n'...
'   no_noise                                                        \n'...
'}                                                                  \n'...
],...
eu(1),eu(2),eu(3),...
ev(1),ev(2),ev(3),...
a(1)-d*ew(1),...
a(2)-d*ew(2),...
a(3)-d*ew(3),...
-(uoff+0.5)*par.du,...
(par.nu-uoff-0.5)*par.du,...
par.nu,...
-(voff+0.5)*par.dv,...
(par.nv-voff-0.5)*par.dv,...
par.nv,...
par.up,...
par.up,...
CT_scale,...
a(1),...
a(2),...
a(3),...
keV,...
kV,...
mAs);

There are many possibilities for handling multi-line strings in GNU Octave. Take the following as one of them:

sprintf("                     \
This is the first line\n      \
and this is the second\n      \
The square root of %d is %d\n \
", 4, 2)

The \\ character is the string breaker , you have to use it whenever you desire to continue the string on the next script line.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM