MATLAB: Problem with conditional code and missing data

codeexistguiisemptyMATLABmatrixuitable

Can this be used?
if (size(cost,1) == 2 && size(limit,1) == 2)
Because I want to take the data from cost table and limit table. The cost table is 4 by 3 table and limit table is 4 by 2 table. So I want to take the data (which are input from user) from limit table. I have this coding:
if P1 < limit(1,1)
P1 = limit(1,1);
lambdanew = P1*2*cost(1,3) + cost(1,2);
I can only execute my program only if the user insert the data into limit table but if the user did not insert the data, so it will be an error saying this:
Index exceeds matrix dimensions.
Error in ==> fyp_editor>Mybutton_Callback at 100
if P1 < limit(1,1)
So my question is how I want to make if statement for the limit table if the user did not enter the data? Is it limit(0)? limit = 0? limit == 0??

Best Answer

I think you will want the ISEMPTY function, or the EXIST function. If there might not be any data in limit, then check if it is empty first. If limit might not be defined, check if it exists first.
if ~isempty(limit)
% Code here, limit has data.
else
% Code here, limit has no data.
end
Or (depending on how you are doing things):
if exist('limit','var')
% Code here, limit exists.
else
% Code here, limit does not exist.
end
Related Question