MATLAB Answers

Compare GUI table elements

1 view (last 30 days)
Hello! I have a GUI with a table, in my cell edit callback i want to compare two elements from different tables. If the elements are numbers and equal the output should be "OK" and if they are different it should be "not equal". If both elements are empty the output should also be OK and if only one is empty "not equal". If the elements are characters or something else the output should be "error".
It´s easy to see if the elements are equal when they are numbers and if both are empty. But i´m struggling to also check if they contain characters.
This is my code right now:
%---Executes on edit in CAMPosVelStatus table.
function table_CAMPosVelStatus_CellEditCallback(hObject, eventdata, handles)
handles.CAMPosVelTable = get(handles.table_CAMPosVel,'Data');
handles.CAMPosVelStatusTable = get(handles.table_CAMPosVelStatus,'Data');
editRow = eventdata.Indices(1);
editCol = eventdata.Indices(2);
if editCol == 3 || editCol == 4
slPosVelIn = cell2mat(handles.CAMPosVelTable(editRow, editCol-1))
posVel = cell2mat(handles.CAMPosVelStatusTable(editRow, editCol))
if isempty(posVel)
posVel = [];
end
if isempty(slPosVelIn)
slPosVelIn = [];
end
if (isequal(posVel, slPosVelIn) && isnumeric(posVel))
handles.CAMPosVelStatusTable(editRow, 5) = cellstr(['OK']);
set(handles.table_CAMPosVelStatus,'Data', handles.CAMPosVelStatusTable);
else
handles.CAMPosVelStatusTable(editRow, 5) = cellstr(['Not equal']);
set(handles.table_CAMPosVelStatus,'Data', handles.CAMPosVelStatusTable);
end
end
but it feels messy and it feels like there should be a better way to handle GUI tables. This code works except if both element are the same character, then the output is "OK" when it should be "error" or something. I don't know how to check this. I tried with isnumeric but even if the table element is a number isnumeric say it isn´t.

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 10 Jun 2016
Using cell2mat() already makes assumptions about the data type of what is stored in the cells. You need to leave the items in cell array form and do the comparisons. For example:
first = handles.CAMPosVelTable(editRow, editCol-1);
second = handles.CAMPosVelStatusTable(editRow, editCol);
matches_okay = cellfun(@(C1,C2) isnumeric(C1) && isnumeric(C2) && isequal(C1, C2), first, second);
nomatch_locations = find(~matches_okay);
if ~isempty(nomatch_locations)
fprintf('match failure at offsets: ');
fprintf('%d ', nomatch_locations);
fprintf('\n');
else
fprintf('Everything matches\n');
end
This can be simplified for the case where editRow and editCol are scalars:
first = handles.CAMPosVelTable{editRow, editCol-1};
second = handles.CAMPosVelStatusTable{editRow, editCol};
matches_okay = isnumeric(first) && isnumeric(second) && isequal(first, second);
if ~matches_okay
fprintf('Match error\n');
else
fprintf('Everything matches\n');
end

  1 Comment

Joakim Magnusson
Joakim Magnusson on 10 Jun 2016
Thank you, it kind of works. As i said i have two tables and i´m doing this check in both table. The idea is to compare the element at (i,i) in the first table with the element at (i,i) in the second table when that element is edited. If i only edit in one of the tables everything is fine after implementing your code. But if i edit in the first table, then the second and then the first again "isnumeric(first)" is zero even if it is a number and i don´t understand why. Must have something to do with when i edit the second table I use
set(handles.table_CAMPosVelStatus,'Data', handles.CAMPosVelStatusTable);
(Because i change something in a column in the first table for which i dont do this check in the CellEditCallback) Also if i edit in the first table the line
isnumeric(second)
this is zero even if it contains a number. Do you know what the reason could be?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!