How can I use a string as a function parameter?

3 vues (au cours des 30 derniers jours)
Tyler Couser
Tyler Couser le 14 Fév 2018
I'm reading a bunch of these strings from a .txt file, and I want to use these strings to make complex numbers.
i.e. I have a string mc = '(1., 0.)' and I want to use that as the parameters for the complex function so I get complex(1.,0.) or complex((1., 0.)).
UPDATE: I figured out that I can use the sscanf function, but I'm still wondering if there is a more efficient way of doing this.

Réponses (1)

Star Strider
Star Strider le 14 Fév 2018
The easiest way is probably to use sscanf:
mc = '(1.2, 0.)';
mn = sscanf(mc, '(%f,%f)')';
mz = complex(mn(:,1), mn(:,2))
Experiment to get the result you want.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by