Index from Matching Element in Table

43 views (last 30 days)
My overall goal is to replace data in a specific row by input data. (In App Designer, I have three input boxes [string, num, num], and when a button is pushed, I want this data to populate into an exisitng UITable.)
Currently, my table has Column A (Names), Column B (Start), and Column C (End). This is what I is happening/I want to happen:
  1. A user selects 'jumping' from Column A displayed in a ListBox. (This is working)
  2. User inputs NewName, NewStart, NewEnd. (Working) User presses okay.
  3. NewName should replace jumping, NewStart replace jumping start and NewEnd replace jumping end. (Not working)
My idea:
function OkayButtonPushed(app,event)
EditData(app);
end
function editdata(app)
%Find 'jumping' in Column A
%Index_Edit_Row = index of jumping
%[Index_Edit_Row, 2] = NewStart
%[Index_Edit_Row, 3] = NewEnd
end
Later, I want to be able to Delete the selected data row ('jumping' row), but I figure if I know how to edit it, it's a bit easier to delete.
I can't seem to find anything that will find a certain string in a column, then give me the index of it. I think strcmp will tell you if it matches or not, but that's not exactly what I want.
Thank you!

Accepted Answer

Adam Danz
Adam Danz on 7 Aug 2019
% Get data from UITable
UIdata = app.UITable.Data;
% Find row of column 1 that matches "jumping"
% *note, strcmpi() is not case sensitive. If you want
% case sensitivity, use strcmp()
rowIdx = strcmpi(UIdata(:,1), 'jumping');
% Update columns 2 and 3 of the 'jumping' row
UIdata(rowIdx,[2,3]) = {999,9999};
% Load the updated data back into the UITable
app.UITable.Data = UIdata;
"Later, I want to be able to Delete the selected data row"
UIdata = app.UITable.Data;
rowIdx = strcmpi(UIdata(:,1), 'jumping');
UIdata(rowIdx,:) = []; % <---- delete row
app.UITable.Data = UIdata;
  5 Comments
Adam Danz
Adam Danz on 8 Aug 2019
This is hard to debug remotely. Could you put a break point at that line, execute the function, then save the following variables to a mat file and attach it?
  • UIdata
  • rowIdx
  • app.NewName
  • app.NewStart
  • app.NewDuration
NewName = app.NewName;
NewStart = app.NewStart;
NewDuration = app.NewDuration;
save('debugData.mat', 'UIdata', 'rowIdx', 'NewName', 'NewStart', 'NewDuration')
Erika Hathaway
Erika Hathaway on 12 Aug 2019
Hi Adam,
Thank you for your help!
I ended up using contains to create a logical array. Then I created a loop where if it comes to the index for which the index in the array is true, it chanes the values of the data.

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer 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!