# how to convert cell vector to char vector?

1 visualizzazione (ultimi 30 giorni)
Luca Tognetti il 6 Dic 2022
Modificato: DGM il 6 Dic 2022
hi, i would like to convert a cell vector which is:
1509
1803
'V20E'
1706
1706
'V21N'
1507
1927
to a char vector. then i need to "read" only the second and third character, for example from 1509 to 50.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (1)

DGM il 6 Dic 2022
Modificato: DGM il 6 Dic 2022
If there's little control over what's in the cell array, here's one way. This can probably be simplified.
C = {1509; 1803; 'V20E'; 1706; 1706; 'V21N'; 1507; 1927};
charout = repmat(' ',[numel(C) 2]); % preallocate
for k = 1:numel(C)
if isnumeric(C{k}) && isscalar(C{k})
cc = num2str(C{k});
elseif ischar(C{k}) && isvector(C{k})
cc = C{k};
else
error('C(%d) is neither a scalar number nor a char vector',k)
end
charout(k,:) = cc(2:3);
end
charout
charout = 8×2 char array
'50' '80' '20' '70' '70' '21' '50' '92'
If the output needs to be a vector, just use reshape().
charout = reshape(charout.',[],1).'
charout = '5080207070215092'
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Logical in Help Center e File Exchange

R2021a

### Community Treasure Hunt

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

Start Hunting!

Translated by