Undefined function or variable

2 次查看(过去 30 天)
Trung Ngo
Trung Ngo 2019-7-3
评论: Trung Ngo 2019-7-4
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 个评论
Walter Roberson
Walter Roberson 2019-7-3
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.
Trung Ngo
Trung Ngo 2019-7-3
Hi Walter sorry for being unclear, I just update the error message as well as my input file

请先登录,再进行评论。

回答(3 个)

Walter Roberson
Walter Roberson 2019-7-3
编辑:Walter Roberson 2019-7-3
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 个评论
Trung Ngo
Trung Ngo 2019-7-3
编辑:Trung Ngo 2019-7-3
Hi Walter,
Because each loading matrix has a size of 2GB, hence I just want to load them each by each to compare the running time with loading them all at once. By doing that, I want to see which element of lat and lon fall into the loading matrix and conduct the interpolation method. If all is not a good idea for using it, should I remove them and just use
elseif lon>-29.9908326066798 & lon <=0.00916736910585314
for multiple condition if ?

请先登录,再进行评论。


chrisw23
chrisw23 2019-7-4
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 个评论
Walter Roberson
Walter Roberson 2019-7-4
No on the persistent. Just
check_value = 2;
in the place that you have persistent.

请先登录,再进行评论。


Trung Ngo
Trung Ngo 2019-7-3
Fix it by
clearvars;
tic
load('checkcheck.mat');
for i=1:length(lat)
tf_a(i)=check_island(lon(i),lat(i));
end
wtime = toc;
fprintf ( 1, 'Program took %f seconds to run.\n', wtime );
%}

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by