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
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
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
%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
Table1
% 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
Table2
1 个评论
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!