How can I use a string as a function parameter?

3 visualizzazioni (ultimi 30 giorni)
Tyler Couser
Tyler Couser il 14 Feb 2018
Risposto: Star Strider il 14 Feb 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.

Risposte (1)

Star Strider
Star Strider il 14 Feb 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.

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by