Problem with conditional code and missing data

11 views (last 30 days)
slumberk
slumberk on 16 Feb 2011
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

Matt Fig
Matt Fig on 16 Feb 2011
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 Comments
Matt Fig
Matt Fig on 16 Feb 2011
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

Sign in to comment.

More Answers (4)

Matt Tearle
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
slumberk on 16 Feb 2011
I have tried the code. This is my code:
if ~isempty(limit)
%Coding for lambda
lambda1 = num2str(A ./ B);
set(handles.answer1_staticText,'String', lambda1);
%Coding for Optimal Power Generation
string1=mat2str(P1,4);
string2=mat2str(P2,4);
string3=mat2str(P1+P2,4);
set(handles.answer2_staticText,'Max',2)
set(handles.answer2_staticText,'String',...
['P1 = ',string1,char(10),...
'P2 = ',string2,char(10),'PT = ',string3]);
%Coding for Total Cost Generation
C1 = cost(1,1) + (cost(1,2)*P1) + (cost(1,3)*P1*P1);
C2 = cost(2,1) + (cost(2,2)*P2) + (cost(2,3)*P2*P2);
CT = C1 + C2;
string4=mat2str(C1,4);
string5=mat2str(C2,4);
string6=mat2str(C1+C2,4);
set(handles.answer3_staticText,'Max',2)
set(handles.answer3_staticText,'String',...
['C1 = ',string4,char(10),...
'C2 = ',string5,char(10),'CT = ',string6]);
end
It still got the same error. hurmm...
  1 Comment
Matt Tearle
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.

Sign in to comment.


slumberk
slumberk on 16 Feb 2011
sory guyz.. now only i can solve it.. this is the code:
if isempty(limit)
%Coding for lambda
lambda1 = num2str(A ./ B);
set(handles.answer1_staticText,'String', lambda1);
%Coding for Optimal Power Generation
string1=mat2str(P1,4);
string2=mat2str(P2,4);
string3=mat2str(P1+P2,4);
set(handles.answer2_staticText,'Max',2)
set(handles.answer2_staticText,'String',...
['P1 = ',string1,char(10),...
'P2 = ',string2,char(10),'PT = ',string3]);
%Coding for Total Cost Generation
C1 = cost(1,1) + (cost(1,2)*P1) + (cost(1,3)*P1*P1);
C2 = cost(2,1) + (cost(2,2)*P2) + (cost(2,3)*P2*P2);
CT = C1 + C2;
string4=mat2str(C1,4);
string5=mat2str(C2,4);
string6=mat2str(C1+C2,4);
set(handles.answer3_staticText,'Max',2)
set(handles.answer3_staticText,'String',...
['C1 = ',string4,char(10),...
'C2 = ',string5,char(10),'CT = ',string6]);
else if P1 < limit(1,1)
P1 = limit(1,1);
lambdanew = P1*2*cost(1,3) + cost(1,2);
if lambdanew < lambda
P1new = limit(1,2);
string1 = mat2str(P1new,4);
else
P1new = limit(1,1);
string1 = mat2str(P1new,4);
end
Pdtnew = Pdt - P1new;
lambda = num2str((Pdtnew*2*cost(2,3)) + cost(2,2));
set(handles.answer1_staticText,'String', lambda);
P2new = Pdtnew;
P2string = mat2str(P2new,4);
set(handles.answer2_staticText,'Max',2)
set(handles.answer2_staticText,'String',...
['P1 = ',string1,char(10),...
'P2 = ',P2string]);
C1 = cost(1,1) + (cost(1,2)*P1new) + (cost(1,3)*P1new*P1new);
C2 = cost(2,1) + (cost(2,2)*P2new) + (cost(2,3)*P2new*P2new);
CT = C1 + C2;
C1string = mat2str(C2,4);
C2string = mat2str(C2,4);
CTstring = mat2str(C1+C2,4);
set(handles.answer3_staticText,'Max',2)
set(handles.answer3_staticText,'String',...
['C1 = ',C1string,char(10),...
'C2 = ',C2string,char(10),...
'CT = ',CTstring]);
Well before this i put the equation for P1 < limit(1,1) is the top priority.. but then i change it.. then it can execute well.. Why is this happening? Or can anyone help me convert if-else into switch-case-break statement? Because i have tried but I'm still cannot solve it..
  5 Comments
slumberk
slumberk on 16 Feb 2011
@matt fig: i have already download it.. But this one does not have m-figure.

Sign in to comment.


slumberk
slumberk on 16 Feb 2011
@matt: this code does not have a problem.. no problem at all.. but there is an error when initially run the prog.. It says this error:
??? Error using ==> feval Undefined function or method 'answer3_staticText_CreateFcn' for input arguments of type 'double'.
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> fyp_editor at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)fyp_editor('answer3_staticText_CreateFcn',hObject,eventdata,guidata(hObject))
??? Error using ==> struct2handle Error while evaluating uicontrol CreateFcn
??? Error using ==> feval Undefined function or method 'answer2_staticText_CreateFcn' for input arguments of type 'double'.
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> fyp_editor at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)fyp_editor('answer2_staticText_CreateFcn',hObject,eventdata,guidata(hObject))
??? Error using ==> struct2handle Error while evaluating uicontrol CreateFcn
??? Error using ==> feval Undefined function or method 'answer1_staticText_CreateFcn' for input arguments of type 'double'.
Error in ==> gui_mainfcn at 96 feval(varargin{:});
Error in ==> fyp_editor at 42 gui_mainfcn(gui_State, varargin{:});
Error in ==> @(hObject,eventdata)fyp_editor('answer1_staticText_CreateFcn',hObject,eventdata,guidata(hObject))
??? Error using ==> struct2handle Error while evaluating uicontrol CreateFcn
>> but the figure is on my screen and i can execute the program..
  2 Comments
slumberk
slumberk on 17 Feb 2011
@matt: it works!!! thank you very much

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!