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!

 Réponse acceptée

Adam Danz
Adam Danz le 7 Août 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 commentaires

Erika Hathaway
Erika Hathaway le 7 Août 2019
Hi Adam,
Thank you for your help!
I'm getting an error: 'To assign to or create a variable in a table, the number of rows must match the height of the table.'
Here's a version of my code:
UIdata = app.UITable.Data;
rowIdx = strcmpi(UIdata(:,1), app.ListBox.Value);
%app.ListBox.Value is what I earlier refered to as 'jumping'
UIdata(rowIdx,[1,2,3]) = {app.NewName, app.NewStart, app.NewDuration};
%I've also tried the version where I make this line into 3 separate lines
app.UITable.Data = UIdata;
In general, I'm also wondering why you have to take out (?) the data from the UITable in order to play with it. I had to take it out to add another row to the table, but I never understood why.
Adam Danz
Adam Danz le 8 Août 2019
Could you copy-paste the full error message (all of it) and indicate which line is causing the error?
About your second question, it's not that you're taking the data out of the UITable and then putting it back in. I know my code-comments may have suggested that. Think of it this way instead: the table is stored in app.UITable.Data and that's the only place it's stored. The UITable is merely a means of displaying those data. So once you change the data in app.UITable.Data you also need to update the UITable.
Erika Hathaway
Erika Hathaway le 8 Août 2019
Modifié(e) : Erika Hathaway le 8 Août 2019
Here's a picture of the error: . Maine is the name of my app, and OkayButtonPushed is the callback that calls the function that I'm working on. (Edit: The message reads: 'Error using Maine/OkayButtonPushed (line 186) To assign to or create a variable in a table, the number of rows must match the height of the table.')
When I went through my code line-by-line, it threw the error at
UIdata(rowIdx,[1,2,3]) = {app.NewName, app.NewStart, app.NewDuration};
Also, I'm not sure how reliable this is, but when I went through line by line, side by side (debugging) with the Matlab command line could give me the values of rowIdx, which I've only gotten to be 0 or 1...
Thank you!
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 le 12 Août 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by