Compare GUI table elements

1 次查看(过去 30 天)
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.

采纳的回答

Walter Roberson
Walter Roberson 2016-6-10
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 个评论
Joakim Magnusson
Joakim Magnusson 2016-6-10
编辑:Joakim Magnusson 2016-6-10
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?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Data Type Identification 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by