Convert a string in cell into numbers.

조회 수: 4 (최근 30일)
Riccardo Cosmai
Riccardo Cosmai 2017년 4월 1일
편집: the cyclist 2017년 4월 1일
I have: X = 1x1 struct X.T = 27919x1 cell and for example X.T(1,1) = 3,7,12,16 like char. I'd like to have X.T(1,1) like 1x4 double with 4 numeric value. How can I do this? Thank you in advance
  댓글 수: 2
the cyclist
the cyclist 2017년 4월 1일
A couple questions:
Do you mean that X.T(1,1) is a character array that includes the commas, as in
'3,7,12,16'
Or something else?
Is all of X.T like that, such that you want to convert the entire cell array from such character arrays to numeric? Or do you just want to do specific elements that you will index into?
Riccardo Cosmai
Riccardo Cosmai 2017년 4월 1일
yes includes commas. And i want to for example 3 like a specific element, 7 like an other specific element,... then a row 4x1 with numeric elements

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

채택된 답변

the cyclist
the cyclist 2017년 4월 1일
% Defining an example cell array that contains a character array.
a{1,1} = '3,7,12,16';
% Convert the contents of that element to a numeric array
a{1,1} = str2num(a{1,1})
  댓글 수: 2
Riccardo Cosmai
Riccardo Cosmai 2017년 4월 1일
Thank you so much. I resolve with this:
for i=1:27919
X.T{i,1}=str2num(X.T{i,1});
end
the cyclist
the cyclist 2017년 4월 1일
편집: the cyclist 2017년 4월 1일
You can do this more compactly like this:
X.T = cellfun(@str2num,X.T,'UniformOutput',false)
The reason I did not suggest this originally is that I thought you only wanted to convert individual elements, not the whole thing.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by