0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

MATLAB 文字列の右詰め、中央揃え、右詰め

0
Posted at

sprintf 関数を使う方法(標準的な書式設定)

% サンプルテキスト
str = 'MATLAB';

% 10文字の幅で右詰め
res_right = sprintf('%10s', str); 
% 表示結果: '    MATLAB'

% 10文字の幅で左詰め
res_left = sprintf('%-10s', str);
% 表示結果: 'MATLAB    '

compose 関数を使う方法(文字列配列に対応)

str_arr = ["A", "AB", "ABC"];

% 5文字の幅で右詰め
res_right = compose("%5s", str_arr);
% 表示結果: ["    A", "   AB", "  ABC"]

% 5文字の幅で左詰め
res_left = compose("%-5s", str_arr);
% 表示結果: ["A    ", "AB   ", "ABC  "]

pad 関数を使う方法(空白で埋める)

str = "MATLAB";

% 10文字の幅になるように左側を空白で埋める(=右詰め)
res_right = pad(str, 10, 'Left');
% 表示結果: "    MATLAB"

% 10文字の幅になるように右側を空白で埋める(=左詰め)
res_left = pad(str, 10, 'Right');
% 表示結果: "MATLAB    "

% 10文字の幅になるように両側を空白で埋める(=中央揃え)
res_center = pad(str, 10, 'Both');
% 表示結果: "  MATLAB  "
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?