raw={'12_peeter','14_kumar','34_jai','56_papu'}
how to replace this format
a={'peeter-12','kumar-14','jai-34','papu-56'}
but iam try split function
temp=raw;
for i=1:length(temp)
a=strsplict(temp(i),'_');
temp=strcat(temp1(2),'-',temp1(1));
raw(i)=temp;
end
but it gives
??? Undefined function or method 'strsplict' for input arguments of type 'char'.

 Akzeptierte Antwort

Star Strider
Star Strider am 3 Jan. 2017

0 Stimmen

You can do it in one line with the regexprep function:
raw={'12_peeter','14_kumar','34_jai','56_papu'}
a = regexprep(raw, '_', '-')
raw =
'12_peeter' '14_kumar' '34_jai' '56_papu'
a =
'12-peeter' '14-kumar' '34-jai' '56-papu'

3 Kommentare

only '_' will changed, but numeric value cantnot replaced
I got a solution another way
for i=1:length(raw)
str=cell2mat(raw(i));
% raw{i}=strcat(str(isstrprop(str,'alpha')),'-',str(isstrprop(str,'digit')););
a=str(isstrprop(str,'alpha'));
n=str(isstrprop(str,'digit'));
raw{i}=strcat(a,'-',n);
end
‘only '_' will changed, but numeric value cantnot replaced’
My code only changes the ‘_’ to a ‘-’, as it demonstrates. Nothing else changes.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

Stephen23
Stephen23 am 4 Jan. 2017

0 Stimmen

The simplest solution is to use regexprep, which only requires one line and one function call:
>> raw = {'12_peeter','14_kumar','34_jai','56_papu'};
>> out = regexprep(raw,'(\d+)_(\w+)','$2-$1');
And checking the output:
>> out{:}
ans = peeter-12
ans = kumar-14
ans = jai-34
ans = papu-56

Community Treasure Hunt

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

Start Hunting!

Translated by