필터 지우기
필터 지우기

Adding and Deleting Columns in UITable App Designer

조회 수: 34 (최근 30일)
Ratanjot
Ratanjot 2023년 2월 21일
댓글: Ratanjot 2023년 2월 23일
I have created an example app with app designer where it will output the data from the excel sheet with the UITable. The user must have the ability to delete columns 4-8 via the clicking on the checkbox and then also add them back to there respected locations in the Table as it was originally. For example, the user should be able to uncheck columns 4 and 6, which will delete the column name and the data for those columns out of the table and only display columns 1-3,5,7-13.
The problem I am having is that upon deleting the column such as column 4, it doesn't delete the specified column name, but rather the one at the very end (i.e., column 13) and the data for column 4. From there, all the data from the right side of column 4 from the original table will shift one to the left. The solution here would be so that column 4 variable name and data are deleted but the rest is kept in the table. If the user decides to put the column back in the table, it will simply be by checking the same checkbox and it will come back to where it once was. For example, if column 4 was deleted by unchecking the checkbox, then later on checked, it will return as the fourth column in the table and not at the very end like how I have it in the code currently. Thank you for your help.

채택된 답변

VBBV
VBBV 2023년 2월 22일
편집: VBBV 2023년 2월 22일
You can modify the function for columnCheckBox Value Changed as below, by using addvars function
function Column4CheckBoxValueChanged(app, event)
value = app.Column4CheckBox.Value;
if value == 0
app.t_final = removevars(app.UITable.Data,4);
app.t_final.Properties.VariableNames = ["One","Two","Three","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen"];
app.UITable.ColumnName = app.t_final.Properties.VariableNames;
app.UITable.Data = (app.t_final)
elseif value == 1
app.t_final = addvars(app.UITable.Data,table2array(app.t_4),'After','Three');
app.t_final.Properties.VariableNames = ["One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen"];
app.UITable.ColumnName = app.t_final.Properties.VariableNames;
app.UITable.Data = app.t_final;
end
end
  댓글 수: 7
VBBV
VBBV 2023년 2월 23일
Anyways, your question was related to adding and deleting the data to table in App using checkboxes. Please open a new question, if you have further questions
Ratanjot
Ratanjot 2023년 2월 23일
편집: Ratanjot 2023년 2월 23일
If I understand correctly, since I have 5 checkboxes, would I need to follow this same method. This would cause for many possible combinations. I thought that there may have been an easier solution to this. But if not then its okay. I appreciate all the help!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Simon Chan
Simon Chan 2023년 2월 23일
Attached is another apporach for your reference.
The modification includes the followings:
(1) Save the original table in the 'UserData' of the uitable.
app.UITable.UserData = app.UITable.Data;
(2) Assign the column number to 'Tag' of the checkbox for that column. Such as 5 for app.Column5CheckBox
(3) The callback for each checkbox would be almost the same except the name of the checkbox.
The following shows the callback for Column4CheckBox and Column5CheckBox as an example.
% Value changed function: Column4CheckBox
function Column4CheckBoxValueChanged(app, event)
value = app.Column4CheckBox.Value;
idx = ismember(["One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen"],app.UITable.ColumnName);
idx(str2double(app.Column4CheckBox.Tag))=~value;
app.UITable.ColumnName = app.UITable.UserData.Properties.VariableNames(idx);
app.UITable.Data = app.UITable.UserData(:,idx);
end
% Value changed function: Column5CheckBox
function Column5CheckBoxValueChanged(app, event)
value = app.Column5CheckBox.Value;
idx = ismember(["One","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Eleven","Twelve","Thirteen"],app.UITable.ColumnName);
idx(str2double(app.Column5CheckBox.Tag))=~value;
app.UITable.ColumnName = app.UITable.UserData.Properties.VariableNames(idx);
app.UITable.Data = app.UITable.UserData(:,idx);
end
On the other hand, I set all checkboxes to false when the program start.
  댓글 수: 1
Ratanjot
Ratanjot 2023년 2월 23일
Exactly the answer I was looking for. Thanks a lot!!!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by