Weird spaced file output with sprintf
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Sushmitha Kudari
le 27 Avr 2020
Réponse apportée : Sushmitha Kudari
le 27 Avr 2020
I have the following code which is supposed to write out to a .env file.
function write_env(profile_name, depth_and_sound_speed, num_source, source_depth,num_reciever, reciever_depth, freq)
%Function to turn data into a .env file:
% profile_name -- name of the file written out
% depth_and_sound_speed --sorted matrix of increasing depth and
% respective sound speed
% freq -- source frequency
%
depth_bottom = max(depth_and_sound_speed(:,1))
avg_ssp = mean(depth_and_sound_speed(:,1))
depth_and_sound_speed = num2str(depth_and_sound_speed)
copy = zeros(size(depth_and_sound_speed,1), 3);
for i = 1:size(copy,1)
copy(i, :) = [num2str(depth_and_sound_speed(i,1)) num2str(depth_and_sound_speed(i,2)) '/'];
end
header = sprintf(['''' profile_name ''' \t ! TITLE\n' ]);
header = sprintf([header, num2str(freq,'%d') '\t! FREQ (HZ)\n']);
header = sprintf([header, num2str(1) '\t ! NMEDIA\n']);
header = sprintf([header,'''SVF''' '\t !SSPOPT (Analytic or C-linear interpolation)\n']);
header = sprintf([header, num2str(51) ' ' num2str(0) ' ' num2str(depth_bottom), '\t! DEPTH of bottom (m)\n']);
footer = sprintf(['''A''0 \n']);
footer = sprintf([footer, num2str(depth_bottom) ' ' num2str(avg_ssp) ' ' num2str(0) ' ' num2str(1) '\t/\n']);
footer = sprintf([footer, num2str(1) '\t ! NSD \n']) %number of source depth
footer = sprintf([footer, num2str(1000) '/ \t !SD(1:NSD) (m) \n' ])
footer = sprintf([footer, num2str(51) '/t !NRD \n'])
footer = sprintf([footer, num2str(0) ' ' num2str(5000) '/ \t ! RD(1:NRD) (m) \n'])
footer = sprintf([footer, num2str(1000) '\t ! NR \n'])
%footer = sprintf([footer, num2str(])])
fid = fopen(profile_name, 'w');
%insert / at the end of the
fprintf(fid, header);
dlmwrite(profile_name, depth_and_sound_speed, '-append', 'delimiter', ' ', 'precision', 3)
dlmwrite(profile_name, footer, '-append', 'delimiter', ' ', 'precision', 3)
fclose(fid);
end
The footer array keeps having spaces between characters. Is there any way to remove this?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/288055/image.png)
I would like to get it into this format:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/288056/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/288057/image.png)
You can test out the script with the following inputs:
a = [5 6; 2 3; 4 6; 2 5; 6 7; 2 3]
write_env('A.env', a, 1, 1000 , 51, 0, 1600)
0 commentaires
Réponse acceptée
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Characters and Strings dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!