필터 지우기
필터 지우기

How do I convert double to cellstr?

조회 수: 10 (최근 30일)
Dominic Noel Kluck
Dominic Noel Kluck 2024년 1월 22일
답변: Sai Teja G 2024년 1월 22일
I have a vector which coul look like this: A = [2 25 1017]. I want to use these numbers in a legend of my plot. Apparently I need to convert them from double into cellstr. Thats what I get out of the error message: Cell array argument must be a cell array of character vectors. How do I convert double into cellstr?
Thanks in advance

채택된 답변

Star Strider
Star Strider 2024년 1월 22일
Use the compose function (introduced in R2016b) —
A = [2 25 1017];
Ac = compose('%d',A)
Ac = 1×3 cell array
{'2'} {'25'} {'1017'}
.

추가 답변 (1개)

Sai Teja G
Sai Teja G 2024년 1월 22일
Hi Dominic, You can refer to the below code to convert double to cell array.
A = [2 25 1017];
A_str = arrayfun(@(x) num2str(x), A, 'UniformOutput', false)
A_str = 1×3 cell array
{'2'} {'25'} {'1017'}
This code snippet uses arrayfun to apply the num2str function to each element of the array A, converting each number to a string. The 'UniformOutput', false argument is used to output the results in a cell array, which is necessary for the legend function in MATLAB.

카테고리

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

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by