Problem with conditional code and missing data

2 次查看(过去 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??

采纳的回答

Matt Fig
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
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
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
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
slumberk 2011-2-16
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 个评论
Matt Tearle
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
slumberk 2011-2-16
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 个评论
Matt Fig
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
slumberk 2011-2-16
@matt fig: i have already download it.. But this one does not have m-figure.

请先登录,再进行评论。


slumberk
slumberk 2011-2-16
@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 个评论
Matt Tearle
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 CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by