Problem with conditional code and missing data
显示 更早的评论
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??
采纳的回答
更多回答(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
5 个评论
Matt Tearle
2011-2-16
I need clarification. Does this code give an error message or not? If not, what is the problem with it?
slumberk
2011-2-16
slumberk
2011-2-16
Matt Fig
2011-2-16
There are plenty of GUI tutorials out there. Here is one I like ;-).
http://www.mathworks.com/matlabcentral/fileexchange/24861-41-complete-gui-examples
slumberk
2011-2-16
slumberk
2011-2-16
0 个投票
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!)
slumberk
2011-2-17
类别
在 帮助中心 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!