Problem with conditional code and missing data
Show older comments
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??
Accepted Answer
More Answers (4)
Matt Tearle
on 16 Feb 2011
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
on 16 Feb 2011
1 Comment
Matt Tearle
on 16 Feb 2011
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
on 16 Feb 2011
5 Comments
Matt Tearle
on 16 Feb 2011
I need clarification. Does this code give an error message or not? If not, what is the problem with it?
slumberk
on 16 Feb 2011
slumberk
on 16 Feb 2011
Matt Fig
on 16 Feb 2011
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
on 16 Feb 2011
slumberk
on 16 Feb 2011
0 votes
2 Comments
Matt Tearle
on 16 Feb 2011
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
on 17 Feb 2011
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!