Filter löschen
Filter löschen

Function - return array instead of single value

45 Ansichten (letzte 30 Tage)
Dario Walter
Dario Walter am 27 Jun. 2019
Bearbeitet: Stephen23 am 5 Jul. 2019
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 Kommentare
KSSV
KSSV am 27 Jun. 2019
If lat_deg and lon_deg are arrays, it is not suggested to use inequalities >,< etc.
Stephen23
Stephen23 am 27 Jun. 2019
Bearbeitet: Stephen23 am 5 Jul. 2019
"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));

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Time Series Objects finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by