How to get values from a struct?

조회 수: 2 (최근 30일)
asli eylul sert
asli eylul sert 2021년 6월 18일
댓글: asli eylul sert 2021년 6월 18일
Hello, I want to extract the ksi1 and ksi2 values from S structure to that I can evaluate yCO, yCO2, etc. to plot them vs Temperature. But I could not do it. I tried to use A = S.ksi1 but even though I can see every value in the command window, workshop only gives me the final value of A in sym type. So I was wandering How can I extract ksi1 and ksi2 so that I can evaluate yCO, yCO2, etc.with them. Thank you in advance.
clc;
clear;
nCO = 1; %starting mol
nCO2 = 1; %starting mol
nH2 = 2; %starting mol
nCH3OH = 0; %starting mol
nH2O = 1; %starting mol
R = 8.314;%J/molK
deltaGrxn1 = -24.8; %kJ/mol
deltaHrxn1 = -90.2; %kJ/mol
deltaSrxn1 = -0.2; %(deltaGrxn1 - deltaHrxn1)/(-T)
deltaGrxn2 = -28.6; %kJ/mol
deltaHrxn2 = -41.2; %kJ/mol
deltaSrxn2 = 0.042; %(deltaGrxn2 - deltaHrxn2)/(-T)
T = 298:10:800; %Temperature Values
for i = 1:size(T,2)
dG1(i) = deltaHrxn1 - T(i) * deltaSrxn1;
Ka1(i) = exp(-dG1(i)/(R*T(i)));
dG2(i) = deltaHrxn2 - T(i) * deltaSrxn2;
Ka2(i) = exp(-dG2(i)/(R*T(i)));
syms ksi1 ksi2
eqn = (((ksi1)./(1-ksi1-ksi2))./((2-2*ksi1-ksi2)/(4-2*ksi1))^2)==Ka1(i);(((1+ksi2).*(2-2*ksi1-ksi2))./((1-ksi2).*(1+ksi2)))==Ka2(i);
S = vpasolve(eqn,ksi1,ksi2);
yCO = ((1 - ksi1 - ksi2)./(4 - 2*ksi1));
yCO2 = ((1 + ksi2)./(4 - 2*ksi1));
yH2 = ((2 - 2*ksi1 - ksi2)./(4 - 2*ksi1));
yCH3OH = ((ksi1)./(4-2*ksi1));
yH2O = ((1-ksi2)./(4-2*ksi1));
end

채택된 답변

Landan Rubenstein
Landan Rubenstein 2021년 6월 18일
Hello,
In your code, S is always a scalar, so A = S.ksi1 should be sufficient to return the scalar value for ksi1. If instead you were recording S on each loop with
S(i) = vpasolve(eqn,ksi1,ksi2);
you could extract each of the S(i).ksi1 values into a vector with
A = [S.ksi1];
This will return a sym vector.
This works because S.x where S is a non-scalar struct returns a comma-seperated list containing the set of values of x (see https://www.mathworks.com/help/matlab/matlab_prog/comma-separated-lists.html), and a vector is formed by enclosing a comma-seperated list with square brackets ([]).
I hope this helps.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by