Compare GUI table elements
    3 views (last 30 days)
  
       Show older comments
    
    Joakim Magnusson
      
 on 10 Jun 2016
  
    
    
    
    
    Edited: Joakim Magnusson
      
 on 10 Jun 2016
            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
Accepted Answer
  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
More Answers (0)
See Also
Categories
				Find more on Data Type Identification in Help Center and File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
