Function - return array instead of single value
조회 수: 37 (최근 30일)
이전 댓글 표시
Hey,
I have the following problem: The function stated below has an input of "lat_deg" and "long_deg" arrays. However, the output is currenty a single value. Why is that?
Thanks for your help!
function [foldername,filename,map_name,ref_name] = evaluatemapname(lat_deg,long_deg )
%Check if we need to add zeros in front of the expression, depending on
%the length of the number
if(abs(lat_deg) < 10)
map_lat = sprintf('0%d',abs(lat_deg));
else
map_lat = sprintf('%d',abs(lat_deg));
end
if(abs(long_deg) < 10)
map_long = sprintf('00%d',abs(long_deg));
elseif(abs(long_deg) < 100)
map_long = sprintf('0%d',abs(long_deg));
else
map_long = sprintf('%d',abs(long_deg));
end
%Norht, East
if(lat_deg >= 0 & long_deg >= 0)
foldername = sprintf('ASTGTM2_N%sE%s',map_lat,map_long);
filename = sprintf('ASTGTM2_N%sE%s_dem.tif',map_lat,map_long);
map_name = sprintf('Map_N%sE%s',map_lat,map_long);
ref_name = sprintf('R_N%sE%s',map_lat,map_long);
end
%Norht, West
if(lat_deg >= 0 & long_deg < 0)
foldername = sprintf('ASTGTM2_N%sW%s',map_lat,map_long);
filename = sprintf('ASTGTM2_N%sW%s_dem.tif',map_lat,map_long);
map_name = sprintf('Map_N%sW%s',map_lat,map_long);
ref_name = sprintf('R_N%sW%s',map_lat,map_long);
end
%South, West
if(lat_deg < 0 & long_deg < 0)
foldername = sprintf('ASTGTM2_S%sW%s',map_lat,map_long);
filename = sprintf('ASTGTM2_S%sW%s_dem.tif',map_lat,map_long);
map_name = sprintf('Map_S%sW%s',map_lat,map_long);
ref_name = sprintf('R_S%sW%s',map_lat,map_long);
end
%South, East
if(lat_deg < 0 & long_deg >= 0)
foldername = sprintf('ASTGTM2_S%sE%s',map_lat,map_long);
filename = sprintf('ASTGTM2_S%sE%s_dem.tif',map_lat,map_long);
map_name = sprintf('Map_S%sE%s',map_lat,map_long);
ref_name = sprintf('R_S%sE%s',map_lat,map_long);
end
end
댓글 수: 2
KSSV
2019년 6월 27일
If lat_deg and lon_deg are arrays, it is not suggested to use inequalities >,< etc.
Stephen23
2019년 6월 27일
편집: Stephen23
2019년 7월 5일
"However, the output is currenty a single value. Why is that?"
If you expect to be able to supply arrays as the inputs then you will need to loop over their values (either explicitly or implictly) to create separate output strings (i.e. character vectors in a cell array, or string arrays). You could use a loop, or arrayfun, or compose, etc.
Note that you should read the sprintf documentation to understand what your code is doing now, and try some simple example with arrays, e.g.:
>> sprintf('%d,',1:4)
ans = 1,2,3,4,
Reading the documentation would also help you to simplify your code, e.g. replacing this
if(abs(long_deg) < 10)
map_long = sprintf('00%d',abs(long_deg));
elseif(abs(long_deg) < 100)
map_long = sprintf('0%d',abs(long_deg));
else
map_long = sprintf('%d',abs(long_deg));
end
with much simpler:
map_long = sprintf('%03d',abs(long_deg));
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Time Series Objects에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!