필터 지우기
필터 지우기

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

조회 수: 2 (최근 30일)
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.

채택된 답변

Walter Roberson
Walter Roberson 2018년 12월 25일
fprintf('legText = cellstr(%s);\n', mat2str(string(legText)))
or
fprintf('legText = {'); fprintf('''%s'',', legText{:}); fprintf('};\n');
  댓글 수: 1
Jessica Hiscocks
Jessica Hiscocks 2018년 12월 26일
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',};

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by