Undefined function or variable
1 view (last 30 days)
Show older comments
Hi all,
I am calling a function and receiving this error, my dummy_run_check_island.m (main file) is:
clearvars;
tic
load('checkcheck.mat');
tf_result=check_island(lon,lat);
wtime = toc;
fprintf ( 1, 'Program took %f seconds to run.\n', wtime );
%}
My check_island.m (calling function) is:
function [output] = check_island(lon,lat)
%Input Lon [-180 180]-format and Lat [-90 90]-format
%lon=-100;
%lat=50;
%lon =133.735508667306;
%lat =47.8022743515138;
if all(lon>=-180.009166666889 & lon <=-149.99083260668)
if all(lat<=60.0091668606037 & lat >= 29.9908328003949)
L = load('110_size.mat');
check_value = 1;
elseif all(lat<29.9908328003949 & lat >= -0.00916669110301882)
L = load('210_size.mat');
check_value = 1;
elseif all(lat<-0.00916669110301882 & lat >= -30.009166691103)
L = load('310_size.mat');
check_value = 1;
elseif all(lat<-30.009166691103 & lat >= -60.0091670543188)
L = load('410_size.mat');
check_value = 1;
end
elseif all(lon>-149.99083260668 & lon <=-119.99083260668)
if all(lat<=60.0091668606037 & lat >= 29.9908328003949)
L = load('120_size.mat');
check_value = 1;
elseif all(lat<29.9908328003949 & lat >= -0.00916669110301882)
check_value = 2;
elseif all(lat<-0.00916669110301882 & lat >= -30.009166691103)
L = load('320_size.mat');
check_value = 1;
elseif all(lat<-30.009166691103 & lat >= -60.0091670543188)
check_value = 2;
end
elseif all(lon>-119.99083260668 & lon <=-89.9908326066798)
if all(lat<=60.0091668606037 & lat >= 29.9908328003949)
L = load('130_size.mat');
check_value = 1;
elseif all(lat<29.9908328003949 & lat >= -0.00916669110301882)
L = load('230_size.mat');
check_value = 1;
elseif all(lat<-0.00916669110301882 & lat >= -30.009166691103)
L = load('330_size.mat');
check_value = 1;
elseif all(lat<-30.009166691103 & lat >= -60.0091670543188)
check_value = 2;
end
elseif all(lon>-89.9908326066798 & lon <=-59.9908326066798)
if all(lat<=60.0091668606037 & lat >= 29.9908328003949)
L = load('140_size.mat');
check_value = 1;
elseif all(lat<29.9908328003949 & lat >= -0.00916669110301882)
L = load('240_size.mat');
check_value = 1;
elseif all(lat<-0.00916669110301882 & lat >= -30.009166691103)
L = load('340_size.mat');
check_value = 1;
elseif all(lat<-30.009166691103 & lat >= -60.0091670543188)
L = load('440_size.mat');
check_value = 1;
end
elseif all(lon>-59.9908326066798 & lon <=-29.9908326066798)
if all(lat<=60.0091668606037 & lat >= 29.9908328003949)
L = load('150_size.mat');
check_value = 1;
elseif all(lat<29.9908328003949 & lat >= -0.00916669110301882)
L = load('250_size.mat');
check_value = 1;
elseif all(lat<-0.00916669110301882 & lat >= -30.009166691103)
L = load('350_size.mat');
check_value = 1;
elseif all(lat<-30.009166691103 & lat >= -60.0091670543188)
L = load('450_size.mat');
check_value = 1;
end
elseif all(lon>-29.9908326066798 & lon <=0.00916736910585314)
if all(lat<=60.0091668606037 & lat >= 29.9908328003949)
L = load('160_size.mat');
check_value = 1;
elseif all(lat<29.9908328003949 & lat >= -0.00916669110301882)
L = load('260_size.mat');
check_value = 1;
elseif all(lat<-0.00916669110301882 & lat >= -30.009166691103)
L = load('360_size.mat');
check_value = 1;
elseif all(lat<-30.009166691103 & lat >= -60.0091670543188)
L = load('460_size.mat');
check_value = 1;
end
elseif all(lon>0.00916736910585314 & lon <=30.0091674417288)
if all(lat<=60.0091668606037 & lat >= 29.9908328003949)
L = load('170_size.mat');
check_value = 1;
elseif all(lat<29.9908328003949 & lat >= -0.00916669110301882)
L = load('270_size.mat');
check_value = 1;
elseif all(lat<-0.00916669110301882 & lat >= -30.009166691103)
L = load('370_size.mat');
check_value = 1;
elseif all(lat<-30.009166691103 & lat >= -60.0091670543188)
L = load('470_size.mat');
check_value = 1;
end
elseif all(lon>30.0091674417288 & lon <=60.0091673933202)
if all(lat<=60.0091668606037 & lat >= 29.9908328003949)
L = load('180_size.mat');
check_value = 1;
elseif all(lat<29.9908328003949 & lat >= -0.00916669110301882)
L = load('280_size.mat');
check_value = 1;
elseif all(lat<-0.00916669110301882 & lat >= -30.009166691103)
L = load('380_size.mat');
check_value = 1;
elseif all(lat<-30.009166691103 & lat >= -60.0091670543188)
L = load('480_size.mat');
check_value = 1;
end
elseif all(lon>60.0091673933202 & lon <=90.0091673933202)
if all(lat<=60.0091668606037 & lat >= 29.9908328003949)
L = load('190_size.mat');
check_value = 1;
elseif all(lat<29.9908328003949 & lat >= -0.00916669110301882)
L = load('290_size.mat');
check_value = 1;
elseif all(lat<-0.00916669110301882 & lat >= -30.009166691103)
L = load('390_size.mat');
check_value = 1;
elseif all(lat<-30.009166691103 & lat >= -60.0091670543188)
L = load('490_size.mat');
check_value = 1;
end
elseif all(lon>90.0091673933202 & lon <=120.00916739332)
if all(lat<=60.0091668606037 & lat >= 29.9908328003949)
L = load('191_size.mat');
check_value = 1;
elseif all(lat<29.9908328003949 & lat >= -0.00916669110301882)
L = load('291_size.mat');
check_value = 1;
elseif all(lat<-0.00916669110301882 & lat >= -30.009166691103)
L = load('391_size.mat');
check_value = 1;
elseif all(lat<-30.009166691103 & lat >= -60.0091670543188)
L = load('491_size.mat');
check_value = 1;
end
elseif all(lon>120.00916739332 & lon <=150.00916739332)
if all(lat<=60.0091668606037 & lat >= 29.9908328003949)
L = load('192_size.mat');
check_value = 1;
elseif all(lat<29.9908328003949 & lat >= -0.00916669110301882)
L = load('292_size.mat');
check_value = 1;
elseif all(lat<-0.00916669110301882 & lat >= -30.009166691103)
L = load('392_size.mat');
check_value = 1;
elseif all(lat<-30.009166691103 & lat >= -60.0091670543188)
L = load('492_size.mat');
check_value = 1;
end
elseif all(lon>150.00916739332 & lon <=180.00916739332)
if all(lat<=60.0091668606037 & lat >= 29.9908328003949)
L = load('193_size.mat');
check_value = 1;
elseif all(lat<29.9908328003949 & lat >= -0.00916669110301882)
L = load('293_size.mat');
check_value = 1;
elseif all(lat<-0.00916669110301882 & lat >= -30.009166691103)
L = load('393_size.mat');
check_value = 1;
elseif all(lat<-30.009166691103 & lat >= -60.0091670543188)
L = load('493_size.mat');
check_value = 1;
end
end
% Interpolate method:
if check_value == 1
tf = interp2(L.x,L.y,L.is_land_uint8,lon,lat,'cubic');
elseif check_value == 2
tf = 0;
end
output=tf;
end
Thank you for your time reading my concern
Update 1: Thanks Walter, I just updated my checkcheck.mat
Error message is as below:
>> dummy_run_check_island
Undefined function or variable 'check_value'.
Error in check_island (line 188)
if check_value == 1
Error in dummy_run_check_island (line 6)
tf_a(i)=check_island(lon,lat);
2 Comments
Walter Roberson
on 3 Jul 2019
You do not provide us with your .mat file, and you do not show us which variable is undefined.
I predict that it is check_value . With your checks for all(), and with the elseif() without a final else(), it is not certain that check_value will always be assigned to.
Answers (3)
Walter Roberson
on 3 Jul 2019
Edited: Walter Roberson
on 3 Jul 2019
K>> [min(lon),max(lon)]
ans =
58.1591322164013 166.237919627731
Now compare to
elseif all(lon>30.0091674417288 & lon <=60.0091673933202)
Some lon are in that range, but not all of them are, so the elseif is false.
elseif all(lon>60.0091673933202 & lon <=90.0091673933202)
Some lon are in that range, but not all of them are, so the elseif is false.
None of the other ranges are applicable.
It is not clear what result you intend in this case.
It is possible that you should be iterating over all of the ranges, and at each point, creating a mask of which values are in the lon range, and interpolating on those values, putting the results together into a whole array.
Or perhaps it would make sense to just put all of the *_size.mat data into a single array and then interpolate based upon that.
1 Comment
chrisw23
on 4 Jul 2019
The check_value variable will not be initialized and if there's no condition met the error occurs.
Initialize check_value at function start and evaluate why no if-condition is met for the given arguments.
Hope it helps
3 Comments
Walter Roberson
on 4 Jul 2019
No on the persistent. Just
check_value = 2;
in the place that you have persistent.
See Also
Categories
Find more on Transmitters and Receivers in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!