Clear Filters
Clear Filters

Weird spaced file output with sprintf

10 views (last 30 days)
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)

Accepted Answer

Sushmitha Kudari
Sushmitha Kudari on 27 Apr 2020
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)

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!