How can I use a string as a function parameter?

4 views (last 30 days)
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.

Answers (1)

Star Strider
Star Strider on 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.

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!