Weird spaced file output with sprintf

1 回表示 (過去 30 日間)
Sushmitha Kudari
Sushmitha Kudari 2020 年 4 月 27 日
回答済み: Sushmitha Kudari 2020 年 4 月 27 日
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?
I would like to get it into this format:
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)

採用された回答

Sushmitha Kudari
Sushmitha Kudari 2020 年 4 月 27 日
Answer is to change the ' ' to '' in the footer write.
Before:
dlmwrite(profile_name, footer, '-append', 'delimiter', ' ', 'precision', 3)
After:
dlmwrite(profile_name, footer, '-append', 'delimiter', '', 'precision', 3)

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImages, Figures, Axes, Equations, MATLAB Code, and MATLAB Variables についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by