Weird spaced file output with sprintf
조회 수: 1 (최근 30일)
이전 댓글 표시
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)
댓글 수: 0
채택된 답변
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Images, Figures, Axes, Equations, MATLAB Code, and MATLAB Variables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!