I have thousands of string variables from where I want to delete some character (both from start and end). For example I want to convert
'TENSOR 27_SN2837_CSN_PROV1_20130209.0.csv' to CSN_PROV1_20130209
'TENSOR 27_SN2837_CSN_WADC1_20131004.0.csv' to CSN_WADC1_20131004
............
...........
Any suggestions?. Thanks

 채택된 답변

Star Strider
Star Strider 2015년 4월 21일

0 개 추천

There may be more efficient ways to do this, but if all the original strings are the same length, this works:
V = ['TENSOR 27_SN2837_CSN_PROV1_20130209.0.csv'
'TENSOR 27_SN2837_CSN_WADC1_20131004.0.csv'];
for k1 = 1:size(V,1)
I1 = strfind(V(k1,:), 'CSN');
I2 = strfind(V(k1,:), '.0');
W(k1,:) = V(k1,I1:I2-1);
end
producing:
W =
CSN_PROV1_20130209
CSN_WADC1_20131004

댓글 수: 6

Mohammed Kamruzzaman
Mohammed Kamruzzaman 2015년 4월 21일
Thanks Star Strider.
Star Strider
Star Strider 2015년 4월 21일
My pleasure.
Hi Star, How I can add quotes to the above. Thanks for your help I need
"CSN_PROV1_20130209"
"CSN_WADC1_20131004"
This is probably the easiest way:
M = ['CSN_PROV1_20130209';
'CSN_WADC1_20131004'];
for k1 = 1:size(M,1)
double_quotes(k1,:) = sprintf('"%s"',M(k1,:));
single_quotes(k1,:) = sprintf('''%s''',M(k1,:));
end
producing:
double_quotes =
"CSN_PROV1_20130209"
"CSN_WADC1_20131004"
single_quotes =
'CSN_PROV1_20130209'
'CSN_WADC1_20131004'
This keeps them with the quotes in your workspace. If you need to print them to a file with the quotes, specify a file ID with fopen and replace sprintf with fprintf. Remember to use fclose to close the file when you’re finished with it.
As always, my pleasure.
Mohammed Kamruzzaman
Mohammed Kamruzzaman 2015년 4월 22일
you are great. Thank you so much.
Star Strider
Star Strider 2015년 4월 22일
My pleasure.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by