Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.

querying fem structure components

Please login with a confirmed email address before reporting spam

Hi all,

I am just getting into scripting and I have a pretty basic question which I couldn't resolve looking at the manuals (maybe I overlooked the relevant section).

Once a fem structure is defined in a Matlab m file, how do I later query its components of interest? What is the right syntax and/or appropriate functions for doing that?

For example, having defined constants as

fem.const = {'Tm','0[degC]', ...
'dT','0.1[K]', ...
'ld','334[J/g]', ...
'Cp_ice','2.108[kJ/kg/K ]'};

how do I later access the numerical value of, say, Tm? I tried obvious things like myvalue=fem.const.Tm which didn't work. Similarly, assuming I can query this constant, how should I extract or get rid of its units and work with only the numeric value? Is there a COMSOL scripting specific method for doing this or should I use usual Matlab's string manipulation functions...
Thanks much,

Ozgur

8 Replies Last Post Dec 8, 2009, 9:41 a.m. EST
Lars Ottar Aschim Kvåle

Please login with a confirmed email address before reporting spam

Posted: 2 decades ago Sep 28, 2009, 10:31 a.m. EDT
Hi Ozgur,

To access the numerical value of a constant at the command prompt I have used this line;
Tm = str2double(fem.const{find(strcmp(fem.const,'Tm'))+1});

Now if your value contains units I'm not aware of any command to handle this at matlab command prompt. I've simply avoided using units when scripting.

Cheers
Lars Ottar Kvale
Hi Ozgur, To access the numerical value of a constant at the command prompt I have used this line; Tm = str2double(fem.const{find(strcmp(fem.const,'Tm'))+1}); Now if your value contains units I'm not aware of any command to handle this at matlab command prompt. I've simply avoided using units when scripting. Cheers Lars Ottar Kvale

Please login with a confirmed email address before reporting spam

Posted: 2 decades ago Sep 28, 2009, 2:57 p.m. EDT
Hi Lars,

thank you for your response. I also heard from COMSOL support with the following suggestion which is very similar to your approach, FYI.

postglobaleval can be used (after model is updated with meshextend)

fem.xmesh = meshextend(fem);
.
.
Tm = postglobaleval(fem,{'Tm'});

Cheers,
Ozgur
Hi Lars, thank you for your response. I also heard from COMSOL support with the following suggestion which is very similar to your approach, FYI. postglobaleval can be used (after model is updated with meshextend) fem.xmesh = meshextend(fem); . . Tm = postglobaleval(fem,{'Tm'}); Cheers, Ozgur

Ivar KJELBERG COMSOL Multiphysics(r) fan, retired, former "Senior Expert" at CSEM SA (CH)

Please login with a confirmed email address before reporting spam

Posted: 2 decades ago Sep 28, 2009, 4:39 p.m. EDT
Hi there

for a slightly different case I have found an expression too, see the second last (of today) reply to Noemi' Petra's demand : "exporting data from the boundaries",
Use the search field, its the quickest to get there try the keyword strmatch

By the way the "Advanced Search" feature is quite nice to scan the discussions, and to find relevant data, there start to be quite a lot of knowledge in here

Good luck
Ivar
Hi there for a slightly different case I have found an expression too, see the second last (of today) reply to Noemi' Petra's demand : "exporting data from the boundaries", Use the search field, its the quickest to get there try the keyword strmatch By the way the "Advanced Search" feature is quite nice to scan the discussions, and to find relevant data, there start to be quite a lot of knowledge in here Good luck Ivar

Please login with a confirmed email address before reporting spam

Posted: 2 decades ago Sep 29, 2009, 2:27 p.m. EDT
Lars, Ivar and all:

I found that the postglobaleval method I quoted previously in this thread works only after the solution is evaluated, so I reverted back to parsing the values of interest directly from the fem.const structure using Matlab functions.

For your reference, I am including below a little function I wrote that trims the units and extracts the numerical value of the constant of interest.

-- In the Script file:
.
.
fem.const = {'Tm','0[degC]', ...
'dT','0.1[K]', ...
'ld','334[J/g]'};
.
.
% This is the function call to evaluate the numerical value of constant of interest from fem.const

constval=numconst('dT',fem.const)
.
.

-- and here is the function m file numconst.m

function strval=numconst(whichconst,femc)
mystr=(femc{find(strcmp(femc,whichconst))+1}); % find the string
% check if there is a bracket indicative of units
if isempty(strfind(mystr,'['))
strval=str2double(mystr); % no bracket found, convert to number directly
else
strval=str2double(mystr(1:strfind(mystr,'[')-1)); % trim the unit before conversion
% Note there is no unit conversion done here - must ensure that constant
% was already provided in the base units, or extend the function to detect
% units and carry out conversions

end
Lars, Ivar and all: I found that the postglobaleval method I quoted previously in this thread works only after the solution is evaluated, so I reverted back to parsing the values of interest directly from the fem.const structure using Matlab functions. For your reference, I am including below a little function I wrote that trims the units and extracts the numerical value of the constant of interest. -- In the Script file: . . fem.const = {'Tm','0[degC]', ... 'dT','0.1[K]', ... 'ld','334[J/g]'}; . . % This is the function call to evaluate the numerical value of constant of interest from fem.const constval=numconst('dT',fem.const) . . -- and here is the function m file numconst.m function strval=numconst(whichconst,femc) mystr=(femc{find(strcmp(femc,whichconst))+1}); % find the string % check if there is a bracket indicative of units if isempty(strfind(mystr,'[')) strval=str2double(mystr); % no bracket found, convert to number directly else strval=str2double(mystr(1:strfind(mystr,'[')-1)); % trim the unit before conversion % Note there is no unit conversion done here - must ensure that constant % was already provided in the base units, or extend the function to detect % units and carry out conversions end

Please login with a confirmed email address before reporting spam

Posted: 2 decades ago Oct 13, 2009, 4:33 p.m. EDT
Hello,

I have a similar question.
However, I'd like to access the value of constant when it is specified as an expression dependent on other constants.
For instance:

fem.const = {'you','60', ...
'me','sqrt(phi)};

How to get value of me instead of expression?

Thanks a lot for your help

Sorry, I already got it - postmax(fem,'me')

Hello, I have a similar question. However, I'd like to access the value of constant when it is specified as an expression dependent on other constants. For instance: fem.const = {'you','60', ... 'me','sqrt(phi)}; How to get value of me instead of expression? Thanks a lot for your help Sorry, I already got it - postmax(fem,'me')

Please login with a confirmed email address before reporting spam

Posted: 2 decades ago Oct 29, 2009, 6:25 a.m. EDT
Hi,

Interesting post. Do you guys have any recommendations for my very similar problem?

www.comsol.com/community/forums/general/thread/1343/

Regards,
Tom
Hi, Interesting post. Do you guys have any recommendations for my very similar problem? http://www.comsol.com/community/forums/general/thread/1343/ Regards, Tom

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Dec 8, 2009, 9:41 a.m. EST
...yet another problem related with this issue:

www.comsol.com/community/forums/general/thread/2069/

regards

Thomas
...yet another problem related with this issue: http://www.comsol.com/community/forums/general/thread/2069/ regards Thomas

Please login with a confirmed email address before reporting spam

Posted: 1 decade ago Dec 8, 2009, 9:41 a.m. EST
...yet another problem related with this issue:

www.comsol.com/community/forums/general/thread/2069/

regards

Thomas
...yet another problem related with this issue: http://www.comsol.com/community/forums/general/thread/2069/ regards Thomas

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.