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

10 ビュー (過去 30 日間)
Vinay Killamsetty
Vinay Killamsetty 2020 年 2 月 27 日
コメント済み: Stephen23 2020 年 3 月 1 日
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

採用された回答

Stephen23
Stephen23 2020 年 2 月 27 日
編集済み: Stephen23 2020 年 2 月 27 日
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 件のコメント
Vinay Killamsetty
Vinay Killamsetty 2020 年 3 月 1 日
Thank you very much for your response.
\This works
Stephen23
Stephen23 2020 年 3 月 1 日
The follow-up question
indicates that using standard binary/text file formats would be much simpler than this pointlessly complex way of encoding data.

サインインしてコメントする。

その他の回答 (1 件)

Bhaskar R
Bhaskar R 2020 年 2 月 27 日
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 件のコメント
Vinay Killamsetty
Vinay Killamsetty 2020 年 2 月 27 日
編集済み: Vinay Killamsetty 2020 年 2 月 27 日
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
Stephen23
Stephen23 2020 年 2 月 27 日
"We have to add commas and braces to the vector."
Why? What is the purpose of this?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by