Filter löschen
Filter löschen

Using sprintf() command

72 Ansichten (letzte 30 Tage)
Articat
Articat am 24 Jul. 2019
Beantwortet: Vimal Rathod am 29 Jul. 2019
Is this the correct way to use this command? flame_x1 is a matrix.
n = 4;
Fuel = JP5;
%%% Upper Flame
sprintf('%s_xvalues_upper_%d', 'Fuel', n) = flame_x1([upper_flame_width]);
  3 Kommentare
Stephen23
Stephen23 am 24 Jul. 2019
Bearbeitet: Stephen23 am 24 Jul. 2019
"Is this the correct way to use this command?"
The problem is not sprintf, but your concept of dynamically naming variables.
Dynamically naming variables is one way that beginners force themselves into writing slow, complex, obfuscated, buggy code that is hard to debug. Read this to know why:
In contrast indexing is neat, simple, easy to understand, easy to debug, and very efficient (unlike what you are trying to do). You should probably use indexing (e.g. with a cell array or a numeric array).
Note that meta-data (such as indexes and types of fuel) are data and so should be stored in a variable, not in a variable's name. Forcing meta-data into variable names makes them slow and complicated to access:
Possibly the best storage for your data would be a non-scalar structure or a table, both of which would make it trival to store the data and meta-data together in one array:

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Vimal Rathod
Vimal Rathod am 29 Jul. 2019
The sprintf function returns formatted string or a character array which may be assigned to a variable.
In the following code given,
n = 4;
Fuel = JP5;
%%% Upper Flame
sprintf('%s_xvalues_upper_%d', 'Fuel', n) = flame_x1([upper_flame_width]);
In the above code flame_x1 should be on the left side of equals so that it attains the value from sprintf function.
Refer to the sprintf documentation for more details:

Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by