solving symbolic array to output a numerical solution

Hi there, I was wondering if I could get some feedback on a potential solution for this problem. I am trying to solve for ma3 and obtain a numerical value for each value used in the FLcD3 array. However, the output of Ma3 is a struct with fields. How do I get it so the solution is an array with the numerical values? I'm guessing I would need some form of a for loop but i'm not quite sure how to set it up.
PSW = 0.36:0.03:0.69;
L12 = PSW*TL;
D = 0.03;
Ma4 = 1;
P4 = 1.013;
Lc3 = TL-L12;
FLcD3 = 4*f*Lc3/D;
ma3 = sym('ma3', [1 length(PSW)]) ;
eqn1 = (-1./G).*(1-ma3.^-2)+((G+1)./(2.*G)).*log((((G+1)./2).*ma3.^2)./(1+((G-1)./2).*ma3.^2)) == FLcD3;
Ma3 = vpasolve(eqn1,ma3)
%This is the output I get
Ma3 =
ma31: [1×1 sym]
ma32: [1×1 sym]
ma33: [1×1 sym]
ma34: [1×1 sym]
ma35: [1×1 sym]
ma36: [1×1 sym]
ma37: [1×1 sym]
ma38: [1×1 sym]
ma39: [1×1 sym]
ma310: [1×1 sym]
ma311: [1×1 sym]
ma312: [1×1 sym]

Answers (2)

VBBV
VBBV on 1 Jun 2023
Edited: VBBV on 1 Jun 2023
From the reference link given by @madhan ravi, you can use struct2cell function and later convert the cell to double array using cell2mat function. And i think both struct2cell & cell2mat are documented well in Matlab
Ma3 = vpasolve(eqn1,ma3)
cell2mat(struct2cell(Ma3))

5 Comments

Your suggestion did not work for me.
I have
struct2array(something)
in a program that used to run ok but today I got the message
Unrecognized function or variable 'struct2array'.
So I tried the suggestion of VBBV, and replaced by
cell2mat(struct2cell(something))
Unfortunatelly I got a new error:
CELL2MAT does not support cell arrays containing cell arrays or objects.
That´s terrible bad news for me. I cant understand the idea of MatLab of removing existing functions.
What shows up for
which -all struct2cell
built-in (/MATLAB/toolbox/matlab/datatypes/struct/@struct/struct2cell) % struct method /MATLAB/toolbox/parallel/array/distributed/@codistributed/struct2cell.m % codistributed method
It is defined as a method of class struct so it would not be found if your something is not a struct
Thank you very much for your SO QUICK answer!
Now that I read my previous message, I note that it is rather incomplete.
In fact, auxMLStructure is a struct (struct with fields: KNL22: [16×16 sym], with KNL22.mat created by another script). I first create auxMLStructure by loading KNL22.mat and thats when I try to manupulate it that I run into problem:
>> auxMLStructure = load('KNL22.mat');
>> matkElemNL22 = vpa(subs(cell2mat(struct2cell(auxMLStructure))))
Error using cell2mat (line 52)
CELL2MAT does not support cell arrays containing cell arrays or objects.
----
So, in fact , the problem is not with struct2cell but with cell2mat
Why don't you directly apply
matkElemNL22 = vpa(subs(auxMLStructure.KNL22))
?
Side note:
As of R2025a (currently in pre-release), cell2mat() does support sym objects.

Sign in to comment.

Asked:

on 26 Mar 2021

Commented:

on 4 Feb 2025

Community Treasure Hunt

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

Start Hunting!