Questions on errors in GUIDE

2 Ansichten (letzte 30 Tage)
Darren Koh
Darren Koh am 13 Feb. 2018
Kommentiert: Darren Koh am 19 Feb. 2018
EDITED Hi there can someone help me to decipher the problem with my program, I have been receiving these error messages when i input data into my uitable and clicking the push button to run my code 'compare'. My code is as follows and the error message is attached behind it.
>> function Sample2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Sample2 (see VARARGIN)
% Choose default command line output for Sample2
handles.output = hObject;
storedvals = zeros(1,4);
% Update handles structure
guidata(hObject, handles);
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
storedvals = handles.storedvals;
x = 0;
tableData = get(handles.uitable1, 'data');
compare(storedvals(1),storedvals(3));
if flag == 1
x = 'P1 Complies with A, B and C';
end
if flag == 2
x = 'P1 Complies with C and B';
end
if flag == 3
x = 'P1 Complies with A and C';
end
if flag == 4
x = 'P1 Complies with A';
end
if flag == 5
x = 'P1 Complies with B and C';
end
if flag == 6
x = 'P1 Complies with C';
end
if flag == 7
x = 'P1 Complies with C';
end
if flag == 8
x = 'Complies with None';
end
if flag == 9
x = 'P2 Complies with A, B and C';
end
if flag == 10
x = 'P2 Complies with A and B';
end
if flag == 11
x = 'P2 Complies A and C';
end
if flag == 12
x = 'P2 Complies A';
end
if flag == 13
x = 'P2 Complies with B and C';
end
if flag == 14
x = 'P2 Complies with C';
end
if flag == 15
x = 'P2 Complies with C';
end
if flag == 16
x = 'Complies with None';
end
set(handles.uitable1, 'Data', x)
% --- Executes when entered data in editable cell(s) in uitable1.
function uitable1_CellEditCallback(hObject, eventdata, handles)
% hObject handle to uitable1 (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
% Indices: row and column indices of the cell(s) edited
% PreviousData: previous data for the cell(s) edited
% EditData: string(s) entered by the user
% NewData: EditData or its converted form set on the Data property. Empty if Data was not changed
% Error: error string when failed to convert EditData to appropriate value for Data
% handles structure with handles and user data (see GUIDATA)
data = get(hObject, 'data');
indices = eventdata.Indices;
r = indices(:,1);
c = indices(:,2);
linear_index = sub2ind(size(data),r,c);
storedvals(linear_index) = data(linear_index);
handles.storedvals = storedvals;
guidata(hObject, handles);
>> Sample2
Undefined operator '==' for input arguments of type 'cell'.
Error in compare (line 3)
if Proc==1
Error in Sample2>pushbutton1_Callback (line 112)
compare(storedvals(1),storedvals(3));
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Sample2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)Sample2('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
  2 Kommentare
Stephen23
Stephen23 am 14 Feb. 2018
Bearbeitet: Stephen23 am 14 Feb. 2018
The error message shows where the error occurs:
Error in compare (line 3)
if Proc==1
However you did not show is the relevant code: you did not provide compare, and Proc does not occur anywhere in the code that you showed us. If you want help with this then please provide the code where the error actually occurs.
It is also not clear what relevance your question title "Questions on global variables in GUI" has to the question, as the code you showed does not define any global variables.
Also note using multiple if statements is very verbose, and could be replaced by one simpler switch statement:
switch flag
case 1
x = 'P1 Complies with A, B and C';
case 2
x = 'P1 Complies with C and B';
case 3
x = 'P1 Complies with A and C';
...
end
Or even simpler by one cell array and some indexing:
C = {...
'P1 Complies with A, B and C',...
'P1 Complies with C and B',...
'P1 Complies with A and C',...
...
};
x = C{flag};
(although I note that flag is also not defined anywhere in the code that you have shown us).
Darren Koh
Darren Koh am 19 Feb. 2018
Dear Stephen, the code for 'compare' function is as follows. Sorry for the late reply and I appreciate your help.
function flag = compare(Proc, Cyc)
flag = 0;
if Proc==1
if(Cyc>=7 && Cyc<=100)
if(Cyc>=8 && Cyc<=100)
if(Cyc>=9 && Cyc<=100)
flag=1;
else
flag=2;
end
else
if(Cyc>=9 && Cyc<=100)
flag=3;
else
flag=4;
end
end
elseif(Cyc>=8 && Cyc<=100)
if(Cyc>=9 && Cyc<=100)
flag=5;
else
flag=6;
end
elseif(Cyc>=9 && Cyc<=100)
flag=7;
elseif(Cyc<9 || Cyc>100)
flag=8;
end
else
if(Cyc>=3 && Cyc<=7)
if(Cyc>=4 && Cyc<=7)
if(Cyc>=5 && Cyc<=7)
flag=9;
else
flag=10;
end
else
if(Cyc>=5 && Cyc<=7)
flag=11;
else
flag=12;
end
end
elseif(Cyc>=4 && Cyc<=7)
if(Cyc>=5 && Cyc<=7)
flag=13;
else
flag=14;
end
elseif(Cyc>=5 && Cyc<=7)
flag=15;
elseif(Cyc<5 || Cyc>7)
flag=16;
end
end
end

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 19 Feb. 2018
handles.storedvals is a cell array of the data in the uitable, therefore you need cell array indexing to access its contents:
compare(storedvals{1},storedvals{3});
  1 Kommentar
Darren Koh
Darren Koh am 19 Feb. 2018
Hi stephen, I got a new error
Index exceeds matrix dimensions.
Error in Sample2>pushbutton1_Callback (line 111)
eqtcompare2(storedvals{1}, storedvals{5});
Is it due to not properly defining what storedvals is? And i have tried running a sample, shorter code to find out whats the issue and i believe my storedvals has not been defined properly as it only stores the latest value of the cell which has been edited and not storing all the values of the cells which are edited.
For example after keying in two values in the same column, ideally i want it to store both values in storedvals(1) and storedvals(2). However after i tried to access storedvals(1) it is empty whereas only storedvals(2) retains the correct value. I was able to run the code although i was using () instead of {} so Im guessing the problem should be with storedvals definition.
The sample code is here. I am able to run it without issues but it will only display the most recently edited cell, storedvals(2) in this case.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
storedvals = handles.storedvals;
tableData = get(handles.uitable1, 'Data');
tableData(1,2) = storedvals(2);
set(handles.uitable1, 'Data', tableData)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Debugging and Analysis finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by