Problem with conditional code and missing data
4 次查看(过去 30 天)
显示 更早的评论
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??
0 个评论
采纳的回答
Matt Fig
2011-2-16
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
5 个评论
Matt Tearle
2011-2-16
That just clears any variables with those names, so now you know that these don't exist in the workspace. Then T = []; creates an empty array T. So isempty(T) will return true. However, T *exists*. P, on the other hand, doesn't even exist. (That's the difference you were asking about.) Note that isempty(P) would throw an error because P doesn't even exist, in order to pass to isempty. That's why Matt suggested using exist instead.
And, since we're here, it depends on how "limit" is being brought into the workspace of the code snippet you've shared. If this is a function foo(limit) and the user enters just >> foo then limit isn't empty, it doesn't even exist in foo's workspace. In that case, you might also want to investigate nargin:
if nargin<1
% limit doesn't exist
else
% limit exists
end
Matt Fig
2011-2-16
In addition to Matt's comments, do not be afraid to read the help for these or any other function you encounter.
help clear
help exist
help isempty
更多回答(4 个)
Matt Tearle
2011-2-16
You can use size -- note: size(x,1) returns the number of rows [dimension 1], and size(x,2) returns the number of columns [dimension 2] -- but I think what you're asking for is
if ~isempty(limit)
For this kind of error checking you may like to investigate all the "is*" functions. Point your doc browser to: MATLAB -> Functions -> Programming and Data Types -> Data Types -> Data Type Identification -> is*
slumberk
2011-2-16
1 个评论
Matt Tearle
2011-2-16
The *same* error? In the initial question you said the error was "Index exceeds matrix dimensions" that occurred at the line "if P1 < limit(1,1)". I don't see that line in this code, or indeed any subscripted reference into "limit" -- only the initial "if ~isempty(limit)". If that's where the error is occurring, then it's more likely to be "undefined function or variable", meaning that limit doesn't exist, rather than it being empty. In that case, see my comment on Matt Fig's answer.
slumberk
2011-2-16
2 个评论
Matt Tearle
2011-2-16
It looks like the answern_staticText uicontrols have a CreateFcn callback associated with them (but no such callback defined). The CreateFcn property is a callback to a function that is executed when the objects are first created, hence the initial error message, but the ability to continue working (the missing callback is only attempted once).
Given the names, I'm guessing that the interface design was done using GUIDE (rather than by direct programming). In that case, open up the design in GUIDE and look in the Property Editor at the properties for these text boxes. Change the CreateFcn property to nothing. See if that fixes the problem. (I can't think of anything for a simple text box that needs to be executed on creation, so hopefully this won't cause any problems!)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!