How to replace leading zeroes by spaces with regexprep

3 ビュー (過去 30 日間)
Blue
Blue 2020 年 7 月 8 日
コメント済み: madhan ravi 2020 年 7 月 9 日
Hi,
I have a basic question. I simply need to replace the leading zeroes by empty spaces in table T except for the last zero which needs to remain zero. The desired output is output. I know I should probably use regexprep but the exact synthax always throws me off. Its for exportation purposes.
a = {'1231', '0002', '0103', '0000'}';
b = {'000', '000', '000', '000'}';
c = {'0', '0', '0', '0'}';
T = table(a,b,c);
d = {'1231', ' 2', ' 103', ' 0'}';
e = {' 0', ' 0', ' 0', ' 0'}';
f = {'0', '0', '0', '0'}';
output = table(d,e,f);
Thank you,

採用された回答

Stephen23
Stephen23 2020 年 7 月 9 日
編集済み: Stephen23 2020 年 7 月 9 日
>> fun = @(c)regexprep(c,'^0+(?=\d)','${char(double($&)-16)}');
>> out = varfun(fun,T)
out =
Fun_a Fun_b Fun_c
______ _____ _____
'1231' ' 0' '0'
' 2' ' 0' '0'
' 103' ' 0' '0'
' 0' ' 0' '0'
Or
>> fun = @(c)regexprep(c,'^0+(?=\d)','${repmat('' '',1,numel($&))}');
>> out = varfun(fun,T)
out =
Fun_a Fun_b Fun_c
______ _____ _____
'1231' ' 0' '0'
' 2' ' 0' '0'
' 103' ' 0' '0'
' 0' ' 0' '0'
  4 件のコメント
Stephen23
Stephen23 2020 年 7 月 9 日
編集済み: Stephen23 2020 年 7 月 9 日
@madhan ravi: thank you.
"Stephen it would be really great if you could make a separate thread just for regular expression. Like the other threads you made, no pressures indeed!"
So far I have only written the "dynamic variable names tutorial", which mostly consisted of collecting together hundreds of links to related discussions and adding an introduction. Time consuming certainly, but not particularly difficult.
Your suggestion is interesting. How should the tutorial be arranged? What topics need to be covered or explained that are not already covered in the documentation? Perhaps there is already an online tutorial which describes regular expressions similar to MATLAB's, it might be easier to just link to that (if we could identifiy a well-written tutorial).
madhan ravi
madhan ravi 2020 年 7 月 9 日
”How should the tutorial be arranged? What topics need to be covered or explained that are not already covered in the documentation?”
To be honest I don’t know how it could be arranged Stephen. But it could cover for example lots of examples which are not covered in the documentation. I will add up some additional ideas in your new thread as well because at the moment the things I learnt about Regex have slightly faded away.
“Perhaps there is already an online tutorial which describes regular expressions similar to MATLAB's, it might be easier to just link to that (if we could identifiy a well-written tutorial).“
There might be many but it wouldn’t match like you describe the concepts!

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

その他の回答 (2 件)

James Tursa
James Tursa 2020 年 7 月 8 日
編集済み: James Tursa 2020 年 7 月 8 日
One way:
fun = @(x)sprintf(['%' num2str(numel(x)) 'd'],str2double(x));
d = cellfun(fun,a,'uni',false);
e = cellfun(fun,b,'uni',false);
f = cellfun(fun,c,'uni',false);
  1 件のコメント
Blue
Blue 2020 年 7 月 9 日
Hi. Thank you for your input but this is going to be quite slow. Thats also why I dont want to convert everything to double and then use the pad function.

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


Raj Kumar Bhagat
Raj Kumar Bhagat 2020 年 7 月 9 日
編集済み: Raj Kumar Bhagat 2020 年 7 月 9 日
I tried using the basic loop statements. I was able to get the required output. Check if this code helps.
if true
p = eliminatePreleadingzeros(a);
function output = eliminatePreleadingzeros(a)
for i = 1: length(a)
tempoutput = [];
for j =1: length(char(a(i)))
tempchar = char(a(i))
if ~strcmp(tempchar(j),'0')
for k = j:length(tempchar)
tempoutput = [tempoutput, tempchar(k)];
end
break;
end
end
if isempty(tempoutput)
output(i)={'0'};
else
output(i) = {tempoutput};
end
end
output = output';
end
end

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by