Need to access the structure values in numerical form and store it in an array each time the loop runs

Hi all,
I am trying to access the structure values from the followign code and want to store it in the form of an array each time the loop runs. I have tried using stryct2cell and cell2mat but they give certain errors. I need the numerical values of x and y by solving the two equations.
Since I am very new to MATLAB programming, I do not much of an idea what does the structure hold in this case and how can we access the numerical values. Kindly help me.
for i = 1:127
syms x y
sol = solve([(1-x)*(1-exp(-y))+ A32T22_array(i)*(1-x)*exp(-y)+ T2T12_array(i) + R2_array(i) == 1, x + R1_array(i)*(1-x)*exp(-y) == R2_array(i), x>0, y>0], [x,y]);
R12T12_array(i) = solxy(1,1);
ad_array(i) = solxy(2,1);
end

 Accepted Answer

Make some modifications
An example
syms x y
sol = solve([5*x-y y-1])
sol = struct with fields:
x: [1×1 sym] y: [1×1 sym]

3 Comments

Thank you for the suggestions.
I tried as you suggested. However, there is an error for that case:
" Unable to perform assignment because the left and right sides have a different number of elements.
Error in Ge_absorption_coefficient (line 70)
R12T12_array(i) = sol.x; "
What are fields and how are they defined. Is it possible to extract the value of them?
I had extracted for one sample without using the loop and I did get it in fractional terms where I had to convert it into double integer form. However, same did not work for a loop.
Looks like you have several roots
just type in the other line
sol.x
And see how many roots you have. Maybe you need something lke this:
R12T12_array = nan(127,5); % assuming max number of roots: 5
for i = 1:127
% code
n = numel(sol.x);
R12T12_array(i,1:n) = sol.x; % write roots
end
@darova: thank you very much for your help.
Though there was a small mistake in one of the equations, however, what you suggested really helped. I also checked it for a different set of simpler equations to test it. Though I need to learn it much better, I understand a bit of it now, thanks to you.

Sign in to comment.

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Products

Release

R2018b

Asked:

on 2 Aug 2021

Commented:

on 5 Aug 2021

Community Treasure Hunt

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

Start Hunting!