Why is my table getting all the same values when it shouldn't?

39 次查看(过去 30 天)
For some reason my code to automatically fill in a table is running but it's outputting the same value for all the columns. Which it shouldn't be getting and I can't figure out why.
All necessary codes to run this properly are attached and should be run as follows to get a proper output: Lab2PartA -> Select "Example1.txt" when prompted -> DeranAutoTableAttempt
Here is my code:
% Set the value of R as depicted in Table 1 and set C = 1.2 ml/mmHg and run the model
R = [0.50, 0.75, 1, 1.25, 1.5, 1.75, 2];
%% Basline (a1_b) of R is 1.2
a1_b = 1.2;
%Prep to run simulink
C = 1.2;
%Prep Table 1
k = 1;
j = 1 + height(Table1);
R = 0.5:0.25:2;
Z = zeros(size(R));
Table1 = table(R(:),Z(:),Z(:),Z(:),Z(:),...
'VariableNames',{'R (mmHg*s/ml)' 'Pressure Maximum (mmHg)' 'Pressure Minimum (mmHg)' 'Presure Mean (mmHg)' 'Pulse Pressure (max-min) (mmHg)'});
while k ~= j
fprintf("R = %d", R(k))
sim('ArterialModel2E')
run MinMax.m;
MinMaxTab = [maxPModel; minPModel; meanPModel];
Table1{k,2} = MinMaxTab(1,1);
Table1{k,3} = MinMaxTab(2,1);
Table1{k,4} = MinMaxTab(3,1);
Table1{k,5} = Table1{k,3}-Table1{k,2};
k = k+1;
end
Table1
% Now set C equal to the value in Table 2 and set R = 1 ml/mmHg
C = [0.6, 0.9, 1.2, 1.5, 1.8, 2.1, 2.4];
% Baseline (a2_b) of C is 1.2
a2_b = 1.2;
% Prep to run simulink
R = 1;
% Prep Table 2
k = 1;
j = 1 + height(Table1);
C = 0.6:0.3:2.4;
Z = zeros(size(C));
Table2 = table(C(:),Z(:),Z(:),Z(:),Z(:),...
'VariableNames',{'C (mmHg*s/ml)' 'Pressure Maximum (mmHg)' 'Pressure Minimum (mmHg)' 'Presure Mean (mmHg)' 'Pulse Pressure (max-min) (mmHg)'});
while k ~= j
fprintf("C = %d", C(k))
sim('ArterialModel2E')
run MinMax.m;
MinMaxTab = [maxPModel; minPModel; meanPModel];
Table2{k,2} = MinMaxTab(1,1);
Table2{k,3} = MinMaxTab(2,1);
Table2{k,4} = MinMaxTab(3,1);
Table2{k,5} = Table2{k,3}-Table2{k,2};
k = k+1;
end
Table2
Table 1 results should be:
Table 2 should be:
  1 个评论
Torsten
Torsten 2024-9-13,14:59
Are you sure that your Simulink model works with the changed parameters (R and C) that you write to file ?

请先登录,再进行评论。

回答(1 个)

Voss
Voss 2024-9-13,15:25
编辑:Voss 2024-9-13,15:32
The basic problem is that R and C are vectors, and the Simulink model appears to only use the first element in that case.
The solution is to rename the R and C vectors to something else and then define R and C as scalar values - corresponding to each element in turn of the vectors - for each run of the Simulink model.
(Also, you had an error in that you were calculating min-max instead of max-min for the last column of the tables.)
See below. The table values now appear to match the reference tables (with the only difference I notice being the bottommost entry in the rightmost column of the second table. But since 100.0-78.9 is 21.1 and not 21.9 it looks like the reference table is wrong).
(I also chose to use for loops instead of while loops, since they are more natural in situations where the number of iterations is known beforehand, in my opinion.)
GetPQ_modified % modified to hard-code the use of Example1.txt
Minimum AOP Measured = 71.6 mmHg Maximum AOP Measured = 114.4 mmHg Mean AOP Measured = 93.5 mmHg Mean AOQ Measured = 89.7 ml/s Systemic Vascular Resistance (SVR) = 1.042 mmHg.s/ml
%Prep to run simulink
C = 1.2;
%Prep Table 1
R_all = 0.5:0.25:2;
Z = zeros(size(R_all));
Table1 = table(R_all(:),Z(:),Z(:),Z(:),Z(:),...
'VariableNames',{'R (mmHg*s/ml)' 'Pressure Maximum (mmHg)' 'Pressure Minimum (mmHg)' 'Presure Mean (mmHg)' 'Pulse Pressure (max-min) (mmHg)'});
j = numel(R_all);
for k = 1:j
R = R_all(k);
fprintf("R = %d", R)
sim('ArterialModel2E')
run MinMax.m;
MinMaxTab = [maxPModel; minPModel; meanPModel];
Table1{k,2} = MinMaxTab(1,1);
Table1{k,3} = MinMaxTab(2,1);
Table1{k,4} = MinMaxTab(3,1);
Table1{k,5} = Table1{k,2}-Table1{k,3}; % max-min
end
R = 5.000000e-01
Warning: 'Output Port 1' of 'ArterialModel2E/Pressure (Pa) Measured' is not connected.
Minimum AOP Model = 25.5 mmHg Maximum AOP Model = 66.1 mmHg Mean AOP Model = 45.0 mmHg
R = 7.500000e-01
Warning: 'Output Port 1' of 'ArterialModel2E/Pressure (Pa) Measured' is not connected.
Minimum AOP Model = 46.8 mmHg Maximum AOP Model = 88.3 mmHg Mean AOP Model = 67.4 mmHg
R = 1
Warning: 'Output Port 1' of 'ArterialModel2E/Pressure (Pa) Measured' is not connected.
Minimum AOP Model = 68.8 mmHg Maximum AOP Model = 110.6 mmHg Mean AOP Model = 89.9 mmHg
R = 1.250000e+00
Warning: 'Output Port 1' of 'ArterialModel2E/Pressure (Pa) Measured' is not connected.
Minimum AOP Model = 90.9 mmHg Maximum AOP Model = 133.0 mmHg Mean AOP Model = 112.3 mmHg
R = 1.500000e+00
Warning: 'Output Port 1' of 'ArterialModel2E/Pressure (Pa) Measured' is not connected.
Minimum AOP Model = 113.2 mmHg Maximum AOP Model = 155.3 mmHg Mean AOP Model = 134.7 mmHg
R = 1.750000e+00
Warning: 'Output Port 1' of 'ArterialModel2E/Pressure (Pa) Measured' is not connected.
Minimum AOP Model = 135.5 mmHg Maximum AOP Model = 177.7 mmHg Mean AOP Model = 157.2 mmHg
R = 2
Warning: 'Output Port 1' of 'ArterialModel2E/Pressure (Pa) Measured' is not connected.
Minimum AOP Model = 157.8 mmHg Maximum AOP Model = 200.1 mmHg Mean AOP Model = 179.6 mmHg
Table1
Table1 = 7x5 table
R (mmHg*s/ml) Pressure Maximum (mmHg) Pressure Minimum (mmHg) Presure Mean (mmHg) Pulse Pressure (max-min) (mmHg) _____________ _______________________ _______________________ ___________________ _______________________________ 0.5 66.062 25.454 45.006 40.608 0.75 88.323 46.844 67.441 41.479 1 110.62 68.786 89.875 41.833 1.25 132.95 90.939 112.31 42.016 1.5 155.32 113.19 134.74 42.126 1.75 177.69 135.5 157.18 42.198 2 200.08 157.84 179.61 42.249
% Prep to run simulink
R = 1;
% Prep Table 2
C_all = 0.6:0.3:2.4;
Z = zeros(size(C_all));
Table2 = table(C_all(:),Z(:),Z(:),Z(:),Z(:),...
'VariableNames',{'C (mmHg*s/ml)' 'Pressure Maximum (mmHg)' 'Pressure Minimum (mmHg)' 'Presure Mean (mmHg)' 'Pulse Pressure (max-min) (mmHg)'});
j = numel(C_all);
for k = 1:j
C = C_all(k);
fprintf("C = %d", C)
sim('ArterialModel2E')
run MinMax.m;
MinMaxTab = [maxPModel; minPModel; meanPModel];
Table2{k,2} = MinMaxTab(1,1);
Table2{k,3} = MinMaxTab(2,1);
Table2{k,4} = MinMaxTab(3,1);
Table2{k,5} = Table2{k,2}-Table2{k,3}; % max-min
end
C = 6.000000e-01
Warning: 'Output Port 1' of 'ArterialModel2E/Pressure (Pa) Measured' is not connected.
Minimum AOP Model = 50.9 mmHg Maximum AOP Model = 132.1 mmHg Mean AOP Model = 90.0 mmHg
C = 9.000000e-01
Warning: 'Output Port 1' of 'ArterialModel2E/Pressure (Pa) Measured' is not connected.
Minimum AOP Model = 62.5 mmHg Maximum AOP Model = 117.8 mmHg Mean AOP Model = 89.9 mmHg
C = 1.200000e+00
Warning: 'Output Port 1' of 'ArterialModel2E/Pressure (Pa) Measured' is not connected.
Minimum AOP Model = 68.8 mmHg Maximum AOP Model = 110.6 mmHg Mean AOP Model = 89.9 mmHg
C = 1.500000e+00
Warning: 'Output Port 1' of 'ArterialModel2E/Pressure (Pa) Measured' is not connected.
Minimum AOP Model = 72.8 mmHg Maximum AOP Model = 106.4 mmHg Mean AOP Model = 89.8 mmHg
C = 1.800000e+00
Warning: 'Output Port 1' of 'ArterialModel2E/Pressure (Pa) Measured' is not connected.
Minimum AOP Model = 75.5 mmHg Maximum AOP Model = 103.5 mmHg Mean AOP Model = 89.8 mmHg
C = 2.100000e+00
Warning: 'Output Port 1' of 'ArterialModel2E/Pressure (Pa) Measured' is not connected.
Minimum AOP Model = 77.4 mmHg Maximum AOP Model = 101.5 mmHg Mean AOP Model = 89.8 mmHg
C = 2.400000e+00
Warning: 'Output Port 1' of 'ArterialModel2E/Pressure (Pa) Measured' is not connected.
Minimum AOP Model = 78.9 mmHg Maximum AOP Model = 100.0 mmHg Mean AOP Model = 89.8 mmHg
Table2
Table2 = 7x5 table
C (mmHg*s/ml) Pressure Maximum (mmHg) Pressure Minimum (mmHg) Presure Mean (mmHg) Pulse Pressure (max-min) (mmHg) _____________ _______________________ _______________________ ___________________ _______________________________ 0.6 132.12 50.908 90.011 81.216 0.9 117.76 62.459 89.921 55.305 1.2 110.62 68.786 89.875 41.833 1.5 106.36 72.751 89.847 33.613 1.8 103.54 75.46 89.828 28.084 2.1 101.54 77.426 89.815 24.113 2.4 100.04 78.918 89.805 21.125
  1 个评论
Voss
Voss 2024-9-13,15:38
编辑:Voss 2024-9-13,15:41
A cleaner (in my opinion) way to write the first half of that code:
%Prep to run simulink
C = 1.2;
%Prep Table 1
R_all = 0.5:0.25:2;
N = numel(R_all);
Z = zeros(N,1);
Table1 = table(R_all(:),Z,Z,Z,Z,...
'VariableNames',{'R (mmHg*s/ml)' 'Pressure Maximum (mmHg)' 'Pressure Minimum (mmHg)' 'Presure Mean (mmHg)' 'Pulse Pressure (max-min) (mmHg)'});
for k = 1:N
R = R_all(k);
fprintf("R = %g", R)
sim('ArterialModel2E')
MinMax
Table1{k,2:end} = [maxPModel, minPModel, meanPModel, maxPModel-minPModel];
end

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by