主要内容

使用多个交互式控件创建实时脚本

R2026b

以下示例说明如何使用交互式控件在 MATLAB 中可视化患者数据并对这些数据进行研究。使用各种交互式控件,您可以过滤患者列表,然后绘制过滤后的列表的年龄和收缩压,从而突出显示超出指定血压的患者。

该示例使用变量来控制滑块和微调器值以及下拉列表项。例如,要按位置过滤患者列表,请插入下拉列表并选择 locationStrings 变量来填充列表中的项。要按年龄过滤患者列表,请插入滑块,并选择 minAge 和 maxAge 变量作为最小和最大值。要指定阈值收缩压,请插入一个滑微调器并选择 minPressure 和 maxPressure 变量作为最小和最大值。

要仅在按下过滤数据按钮时过滤数据,请将下拉列表、复选框、滑块和编辑字段的运行执行选项设置为 Nothing。

Label configuration menu with the Min value set to the minAge variable, Max value set to the maxAge variable, and the Run execution option set to Nothing.

要查看控件并与之交互,请在浏览器或 MATLAB 中打开此示例。

获取和过滤采样患者数据

使用文件浏览器选择一个包含患者数据的文件,然后基于患者数据创建一个表。使用下拉列表、复选框、滑块和编辑字段指定患者过滤信息,如位置、吸烟状态、年龄和患者姓氏中的字母。使用过滤数据按钮过滤数据。

filename = "patients.xls";
T = readtable(filename);

locationStrings = ["VA Hospital","County General Hospital","St. Mary's Medical Center"];
selectedLocation = locationStrings(1);

isSmoker = true;

maxAge = max(T.Age);
minAge = min(T.Age);
minimumAge = 31;

nameContains = "e";

 

idx = T.Location==selectedLocation & T.Smoker==isSmoker & T.Age>=minimumAge;
if ~strcmp(nameContains,"")
    idx = idx & contains(T.LastName,nameContains);
end
TFiltered = T(idx,:);

绘制过滤后的患者数据

绘制过滤后的患者数据的年龄和收缩压,突出显示超出指定血压的患者。使用微调器指定血压阈值。

minPressure = min(TFiltered.Systolic);
maxPressure = max(TFiltered.Systolic);
thresholdPressure = 123;

TOverThreshold = TFiltered(TFiltered.Systolic>=thresholdPressure,:);

sp1 = scatter(TFiltered.Age,TFiltered.Systolic);
hold on
sp2 = scatter(TOverThreshold.Age,TOverThreshold.Systolic,"red");
hold off

title("Age vs. Systolic Blood Pressure of Patients")
xlabel("Age")
ylabel("Systolic Blood Pressure")

legendText = sprintf("Patients with systolic blood pressure over %d mmHg",thresholdPressure);
legend(sp2,legendText,Location="southoutside")

Figure contains an axes object. The axes object with title Age vs. Systolic Blood Pressure of Patients, xlabel Age, ylabel Systolic Blood Pressure contains 2 objects of type scatter. This object represents Patients with systolic blood pressure over 123 mmHg.