Convert vector to cell array
조회 수: 10 (최근 30일)
이전 댓글 표시
I am trying to use this
xDataNumbers = [2.5 5 7 8 11.5]
as the datanames for a fints
tsobj = fints(dates, data, datanames)
so - the question is: How do I convert a vector with numbers into a "Cell array of data series names" in the form nn.n?
Thanks. A
댓글 수: 3
채택된 답변
Kye Taylor
2013년 6월 18일
The third input to the fints function is a cell array of strings that can be valid MATLAB identifiers. That is, the strings cannot start with numbers and can only contain letters, numbers, and the underscore. As a workaround, you could do this
% convert numeric array to cell, and convert all contents to strings
dataNamesInput = cellfun(@num2str,num2cell(xDataNumbers(:)),'uniformoutput',false);
% make sure first letter is not numeric (it will be 'x')
dataNamesInput = cellfun(@(s)['x',s],dataNamesInput,'uniformoutput',false);
% remove periods and replace with underscores
dataNamesInput = regexprep(dataNamesInput,'\.','_');
and use dataNamesInput as the third input. Someone may suggest using the genvarnames function, but it will replace periods with '0x2'. You may prefer that. In any case, you'll still need the first line of code above that converts numeric array to cell of strings.
댓글 수: 1
Jan
2013년 6월 19일
This:
dataNamesInput = cellfun(@(s)['x',s],dataNamesInput, 'uniformoutput',false);
dataNamesInput = regexprep(dataNamesInput,'\.','_');
can be done faster by:
dataNamesInput = strcat('x', dataNamesInput);
dataNamesInput = strrep('.', '_');
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!