フィルターのクリア

delete the first and last character inside a char

16 ビュー (過去 30 日間)
Alberto Acri
Alberto Acri 2023 年 6 月 26 日
コメント済み: Dyuman Joshi 2023 年 6 月 26 日
Hi. How can I do to delete the 0 and A present in each row of the cell?
values = {'08126A';'05354A';'01406A';'09630A'};
I am using this code but would like to delete only the first 0.
charact = {'08126A';'05354A';'01406A';'09630A'};
pat = num2cell('0A');
charact = erase(charact,pat);

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 6 月 26 日
charact = {'08126A';'05354A';'01406A';'09630A'};
y=regexprep(charact,'0(\d*)A','$1')
y = 4×1 cell array
{'8126'} {'5354'} {'1406'} {'9630'}
  2 件のコメント
Rik
Rik 2023 年 6 月 26 日
You can easily extend this code to work for other leading and trailing characters (and other characters in the middle):
charact = {'08126A';'05354A';'01406A';'09630A'};
y=regexprep(charact,'.(.*).','$1')
y = 4×1 cell array
{'8126'} {'5354'} {'1406'} {'9630'}
Dyuman Joshi
Dyuman Joshi 2023 年 6 月 26 日
That's neat!

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

その他の回答 (1 件)

Sharad
Sharad 2023 年 6 月 26 日
As per my understanding, you are interested in eliminating the 0s and As from each row of the cells.In order to delete the 0 and A present in each row of cell, you might want to follow these steps:
  • Declare the values cell array (as done).
values = {'08126A';'05354A';'01406A';'09630A'};
  • Use the strrep function to replace 0 and A with empty character ''.
% Remove '0' from each cell
values = strrep(values, '0', '');
% Remove 'A' from each cell
values = strrep(values, 'A', '');
  • Display the resultant values cell using 'disp'.
disp(values);
Find the MATLAB strrep documentation here: https://in.mathworks.com/help/matlab/ref/strrep.html
Thank you.
  1 件のコメント
DGM
DGM 2023 年 6 月 26 日
That will strip all zeros from the vectors, not just leading zeros.
values = {'08126A';'05354A';'01406A';'09630A'};
% Remove '0' from each cell
values = strrep(values, '0', '');
% Remove 'A' from each cell
values = strrep(values, 'A', '')
values = 4×1 cell array
{'8126'} {'5354'} {'146' } {'963' }

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

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by