1

I have the following:

ans = 

    '[-1, 0, 1, 0, 0, 0]'

I really want the variable

x = [-1, 0, 1, 0, 0, 0]

How can I convert ans into x?

2 Answers 2

3

Use str2num:

s = '[-1, 0, 1, 0, 0, 0]';
x = str2num(s);

If your input is a cell array:

c = {'[-1, 0, 1, 0, 0, 0]'};
x = str2num(c{1});
Sign up to request clarification or add additional context in comments.

2 Comments

Error using str2num (line 33) Requires string or character array input.
@JDS Maybe your variable is a cell: c = {'[-1, 0, 1, 0, 0, 0]'}? In that case, use x = str2num(c{1})
2

Try this:

a =  '[-1, 0, 1, 0, 0, 0]'

x = str2num(a(2:end-1))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.