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

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

 Respuesta aceptada

Stephen23
Stephen23 el 27 de Feb. de 2020
Editada: Stephen23 el 27 de Feb. de 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.

2 comentarios

Thank you very much for your response.
\This works
The follow-up question
indicates that using standard binary/text file formats would be much simpler than this pointlessly complex way of encoding data.

Iniciar sesión para comentar.

Más respuestas (1)

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)

2 comentarios

Vinay Killamsetty
Vinay Killamsetty el 27 de Feb. de 2020
Editada: Vinay Killamsetty el 27 de Feb. de 2020
Thank you,
But
a is a numeric vector which is resulted from some equations
so, numeric vector don't contain commas or braces in it.
We have to add commas and braces to the vector.
Kindly correct me if am wrong
"We have to add commas and braces to the vector."
Why? What is the purpose of this?

Iniciar sesión para comentar.

Categorías

Productos

Versión

R2019a

Etiquetas

Preguntada:

el 27 de Feb. de 2020

Comentada:

el 1 de Mzo. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by