remove whitespace from cell array

I have a cell array which looks something like this;
'DS 5376'
'FR 8241'
'B 1257'
'BG 09857'
'TR 3017'
I want to remove the central white space (or any white space to that matter) to get the following
'DS5376'
'FR8241'
'B1257'
'BG09857'
'TR3017'
Previous questions and suggested and I have tried using
cell_Array=strrep(cell_Array,' ','')
but to no avail - any ideas as to how I can acheive this?
Many thanks

 採用された回答

Guillaume
Guillaume 2015 年 1 月 9 日

3 投票

If your example with strrep does not succeed, that would be because the whitespace is actually not a space. You can check what it is for sure by getting its ASCII value with:
double(cell_Array{1}(3))
If it is some other sort of whitespace (maybe a tab), you can use:
cell_Array = regexprep(cell_Array, '\s', '') %\s means any of \t, \f, \n, \r or \v
But if it is not, assuming your text is only letters, numbers or underscore, you can remove anything else with:
cell_Array = regexprep(cell_Array, '\W', '')

4 件のコメント

jnaumann
jnaumann 2015 年 1 月 9 日
Hmm.. no success yet. I also tried this
flight_no=cellfun(@strrep,flight_no,' ','');
which gave this error
Error using cellfun
Input #3 expected to be a cell array, was
char instead.
Error in closest_distance_to_SLM (line 5)
flight_no=cellfun(@strrep,flight_no,' ','');
jnaumann
jnaumann 2015 年 1 月 9 日
also your suggestion of
double(cell_Array{1}(3))
gave 32 which is a white space
Guillaume
Guillaume 2015 年 1 月 9 日
What? None of the regexprep succeeded?
Can you show the output of
double(cell_Array{1}) %i.e. the ASCII values of the whole string
The proper syntax for the cellfun would be:
flight_no = cellfun(@(s) strrep(s, ' ', ''), flight_no, 'UniformOutput', false);
But strrep operates on cell array anyway, so there's no need for the cellfun anyway.
jnaumann
jnaumann 2015 年 1 月 9 日
Your correction for the cellfun example worked - thanks a lot!

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

その他の回答 (1 件)

Lev Vitkin
Lev Vitkin 2024 年 1 月 17 日

0 投票

flight_no = cellfun(@(s) s(~isspace(s)), flight_no, 'UniformOutput', false)

カテゴリ

ヘルプ センター および File ExchangeCell Arrays についてさらに検索

質問済み:

2015 年 1 月 9 日

回答済み:

2024 年 1 月 17 日

Community Treasure Hunt

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

Start Hunting!

Translated by