https://kr.mathworks.com/help/matlab/creating_guis/display-items-in-a-tree-app-or-gui.html
트리를 사용하여 데이터를 계층 구조로 표시하는 앱 - MATLAB & Simulink - MathWorks 한국
이 예제의 수정된 버전이 있습니다. 사용자가 편집한 내용을 반영하여 이 예제를 여시겠습니까?
kr.mathworks.com
핵심코드
1) Properties (Access = Private)
프로그램 코드 작성 시 사용할 변수 선언
properties (Access = private) Data end |
2) methods (Access = Private)
2-1) 수정이 가능한 형태
![](https://blog.kakaocdn.net/dn/XuSeY/btsA5e9mW74/XvHP4D3NCZgI5ngYha3BI0/img.png)
function enableForm(app) app.NameEditField.Enable = 'on'; app.AgeEditField.Enable = 'on'; app.GenderDropDown.Enable = 'on'; app.HealthStatusDropDown.Enable = "on"; app.SmokerCheckBox.Enable = 'on'; app.NameEditFieldLabel.Enable = "on"; app.AgeEditFieldLabel.Enable = 'on'; app.GenderDropDownLabel.Enable = 'on'; app.HealthStatusDropDownLabel.Enable = "on"; end |
2-2) 수정이 불가능한 형태
![](https://blog.kakaocdn.net/dn/UW2t4/btsA23tYhL6/JfFHiNIRLY6iQVBVL4wT90/img.png)
function disableForm(app) app.NameEditField.Value = ''; app.AgeEditField.Value = 0; app.GenderDropDown.Value = 'Male'; app.HealthStatusDropDown.Value = "Excellent"; app.SmokerCheckBox.Value = false; app.NameEditField.Enable = "off"; app.AgeEditField.Enable = 'off'; app.GenderDropDown.Enable = 'off'; app.HealthStatusDropDown.Enable = "off"; app.SmokerCheckBox.Enable = 'off'; app.NameEditFieldLabel.Enable = "off"; app.AgeEditFieldLabel.Enable = 'off'; app.GenderDropDownLabel.Enable = 'off'; app.HealthStatusDropDownLabel.Enable = "off"; app.SmokerCheckBox.Enable = 'off'; end |
3) Callback Function
3-1) 앱이 시작될 때 데이터 로딩 및 초기화를 수행하는 기능
function startupFcn(app) t = readtable("patients.xls"); patients = t(strcmp(t.Location, app.CountyGeneralHospitalNode.Text) & t.Smoker & t.Age >44, "LastName"); for j = 1:size(patients, 1) lastname = char(patients{j, 'LastName'}); uitreenode(app.CountyGeneralHospitalNode, "Text", lastname); end patients = t(strcmp(t.Location, app.StMarysMedicalCenterNode.Text) & t.Smoker & t.Age >44, "LastName"); for j = 1:size(patients, 1) lastname = char(patients{j, 'LastName'}); uitreenode(app.StMarysMedicalCenterNode, "Text", lastname); end patients = t(strcmp(t.Location, app.VAHospitalNode.Text) & t.Smoker & t.Age >44, "LastName"); for j = 1:size(patients, 1) lastname = char(patients{j, 'LastName'}); uitreenode(app.VAHospitalNode, "Text", lastname); end t.Properties.RowNames = t.LastName; app.Data = t; end |
3-2) 트리(Tree)의 선택이 변경될 때 호출되는 함수
function TreeSelectionChanged(app, event) selectedNode = app.Tree.SelectedNodes; hospitals = {'County General Hospital','St. Mary''s Medical Center','VA Hospital'}; if ~isempty(find(strcmp(selectedNode.Text, hospitals), 1)) disableForm(app); else enableForm(app); end patientName = selectedNode.Text; if isempty(selectedNode.Children) data = app.Data(patientName,:); if ~isempty(data) app.NameEditField.Value = patientName; app.AgeEditField.Value = data.Age; app.SmokerCheckBox.Value = data.Smoker; app.GenderDropDown.Value = char(data.Gender); app.HealthStatusDropDown.Value = char(data.SelfAssessedHealthStatus); end end end |
3-3) Age가 변경될 때 호출되는 함수
function AgeEditFieldValueChanged(app, event) confirm = uiconfirm(app.UIFigure, "Change Patient Age?","Confirm Change"); patientName = app.Tree.SelectedNodes.Text; if strcmp(confirm, "OK") app.Data.Age(patientName) = app.AgeEditField.Value; else app.AgeEditField.Value = app.Data.Age(patientName); end end |
3-4) Smoker가 변경될 때 호출되는 함수
function SmokerCheckBoxValueChanged(app, event) confirm = uiconfirm(app.UIFigure, "Change Smoker Status?","Confirm Change"); patientName = app.Tree.SelectedNodes.Text; if strcmp(confirm, "OK") app.Data.Smoker(patientName) = app.SmokerCheckBox.Value; else app.SmokerCheckBox.Value = app.Data.Smoker(patientName); end end |
.
3-5) Health Status가 변경될 때 호출되는 함수
function HealthStatusDropDownValueChanged(app, event) confirm = uiconfirm(app.UIFigure, "Change Health Status?","Confirm Change"); patientName = app.Tree.SelectedNodes.Text; if strcmp(confirm, "OK") app.Data.SelfAssessedHealthStatus(patientName) = app.HealthStatusDropDown.Value; else app.HealthStatusDropDown.Value = app.Data.SelfAssessedHealthStatus(patientName); end end |
3-6) Gender가 변경될 때 호출되는 함수
function GenderDropDownValueChanged(app, event) confirm = uiconfirm(app.UIFigure, "Change Patient Gender?","Confirm Change"); patientName = app.Tree.SelectedNodes.Text; if strcmp(confirm, "OK") app.Data.Gender(patientName) = app.GenderDropDown.Value; else app.GenderDropDown.Value = app.Data.Gender(patientName); end end |