필터 지우기
필터 지우기

Weird spaced file output with sprintf

조회 수: 10 (최근 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 CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by