MATLAB Answers

Simplest way to save a vector to a variable with commas and braces

3 views (last 30 days)
Vinay Killamsetty
Vinay Killamsetty on 27 Feb 2020
Commented: Stephen on 1 Mar 2020
How a can I save a numeric vector a= 1 2 3 4 5
as [1,2,3,4,5] (braces and commas are to be added here)
with a simple solution

Accepted Answer

Stephen on 27 Feb 2020
Edited: Stephen on 27 Feb 2020
You can create a character vector:
>> strrep(mat2str(a),' ',',')
ans = [1,2,3,4,5]
which you can save however you want. But this is unlikely to be useful, or a good approach to saving data.

More Answers (1)

Bhaskar R
Bhaskar R on 27 Feb 2020
You can't save the variable data like that but you can store as in string format
a = '[1, 2, 3, 4, 5]'
save matfile a
load matfile
a = str2num(a)
Stephen on 27 Feb 2020
"We have to add commas and braces to the vector."
Why? What is the purpose of this?

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!

Translated by