Clear Filters
Clear Filters

How to send variable in a text string?

18 views (last 30 days)
Prasad Raut
Prasad Raut on 13 Sep 2019
Commented: Walter Roberson on 13 Sep 2019
Hi I am trying to send a variable in a text string
This is the code
mass_a=1;mass_b=2;mass_c=3;
set(gas,'nc7h16:mass_a:,o2:mass_b,n2:mass_c');
This is a part of cantera code I am using, I am trying to set mass fractions of certain species in a gas structure. As per cantera code I am suppose to set mass fractions through text string. but this I won't be able to run it in for loop. hence I wanted to give mass fractions before every run which I have as variables.
I don't know how to send variables in text string so the cantera code can read?
  2 Comments
dpb
dpb on 13 Sep 2019
Is there a set method for whatever the variable gas represents? AFAIK, the MATLAB builtin function only operates on HG2 objects.
However, that's not the problem about writing text strings--just use sprintf or num2str or, if the syntax supports it, the newer string class.
mass_a=1;mass_b=2;mass_c=3;
cmd=sprintf('nc7h16:%d,o2:%d,n2:%d',mass_a,mass_b,mass_c);
set(gas,cmd);
for the specific case with unique variables and hardcoded species (and for the integer values you've chosen; if use more precise values, adjust the numeric format to suit the precision and format needed).
However, it would be more useful to be able to store the various information in arrays and generalize--
masses=[1;2;3];
species={'nc7h16';'o2';'n2'};
fmt=repmat('%s:%d',1,numel(masses));
cmd=[];
for i=1:numel(masses)-1
cmd=[cmd,sprintf('%s:%d,',species{i},mass(i))];
end
cmd=[cmd,sprintf('%s:%d',species{numel(masses)},mass(numel(masses)))];
set(gas,cmd);
Walter Roberson
Walter Roberson on 13 Sep 2019
set() does work on more general objects, and special support for set/get methods is built into MATLAB's class support. But for class support, set() has to have 3 arguments: object, property name, and new value -- with the exception of the case where you are querying the permitted values, such as set(groot, 'Units') to get the list of values of Units that are permitted.

Sign in to comment.

Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!