Effacer les filtres
Effacer les filtres

Converting multiple cells to an equivilent character vector defining the original array

2 vues (au cours des 30 derniers jours)
As part of an app, I need to use a cell array containing characters to generate a string of text that when pasted in the command window would generate the original cell array as a variable.
input example: the 1×4 cell array named 'legText' with the below contents.
{'Indexed'} {'Forsterite'} {'Enstatite'} {'Diopside'}
Desired output (as a character vector)
legText={'Indexed','Forsterite','Enstatite','Diopside'};
I've been doing this by appending to a character vector in a loop without preallocation, but this is pretty inefficient. Is there a better method?
Thanks, Jessica.

Réponse acceptée

Walter Roberson
Walter Roberson le 25 Déc 2018
fprintf('legText = cellstr(%s);\n', mat2str(string(legText)))
or
fprintf('legText = {'); fprintf('''%s'',', legText{:}); fprintf('};\n');
  1 commentaire
Jessica Hiscocks
Jessica Hiscocks le 26 Déc 2018
Thank you, this got me 95% of the way there! It turned out that using the second option, changing to sprintf, and concatenating was the answer- the final comma does not affect the output
code:
temp=[sprintf('legText = {'), sprintf('''%s'',', legText{:}), sprintf('};')];
output:
legText = {'Indexed','Forsterite','Enstatite','Diopside',};

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by