generate a string with the numbers 1:999

16 ビュー (過去 30 日間)
Alberto Acri
Alberto Acri 2023 年 6 月 16 日
編集済み: Mayur 2023 年 6 月 16 日
Hello! I need to generate a string with the numbers 1:999.
I wrote these lines of code:
row_number = 1:999;
column_number = row_number.';
conversion_column_number = num2str(column_number);
I want to eliminate the two spaces that are created before the numbers 1:9 and the space that is created between the numbers 10:99 in conversion_column_number. Is there a solution to this problem?

採用された回答

Malay Agarwal
Malay Agarwal 2023 年 6 月 16 日
編集済み: Malay Agarwal 2023 年 6 月 16 日
Hi, Alberto
Please try the following code:
row_number = 1:999;
column_number = row_number.';
conversion_column_number = string(column_number);
This has the following output:
According to MATLAB documentation, the function num2str() converts the input array to a character array and MATALB makes sure that each element in a character array is of the same size, which is why you're getting the spaces.

その他の回答 (1 件)

Mayur
Mayur 2023 年 6 月 16 日
編集済み: Mayur 2023 年 6 月 16 日
Hi!
I understand that you want to convert 'column_number' to a string without extra spaces.
You can use the 'string' function instead of 'num2str' here.
row_number = 1:999;
column_number = row_number.';
conversion_column_number = string(column_number);
Alternatively, you can try with this.
row_number = 1:999;
column_number = row_number.';
conversion_column_number = "" + column_number;
For more information you can refer the following documentation: https://www.mathworks.com/help/matlab/ref/string.html
  1 件のコメント
Stephen23
Stephen23 2023 年 6 月 16 日
編集済み: Stephen23 2023 年 6 月 16 日
"For more information you can refer the following documentation..."
Rather than linking to the Stateflow toolbox's overloaded STRING function, much better would be a link to the actual MATLAB STRING function:
Always pay attention to the toolbox!

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

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by