from cell array to numeric vector

I have a 1x52 length cell array of years stored in MWDIData_years, i need to get these to be a nummeric vector. I am trying something like
x = cell2mat(MWDIData_years(:));
But it gives me a 52x4 matrix of chars where each adress in the matrix is a single number, ie 1994 becomes 1 9 9 4.
How can i get around this problem ?
Thank you
Thor

 채택된 답변

Mark Whirdy
Mark Whirdy 2012년 12월 16일
편집: Mark Whirdy 2012년 12월 16일

2 개 추천

MWDIData_pop(1,1:5) = {[3752373],[3876616],[4006236],[4140773],[4279524]}; % square brackets in a cellarray indicates that the elements are numeric so if this is your starting point then no need to convert to a numeric, just convert cell to mat
cell2mat(MWDIData_pop(i,:)); % just a cell2mat
If I'm misunderstanding what you're doing here, then please paste in more of your code which GENERATES these cellarrays as the extra bit of clarity there will help in giving you a better response in how to manipulate/convert them to other datatypes.

댓글 수: 1

DoVile Last Name:
DoVile Last Name: 2012년 12월 17일
Cell2mat was all that i needed, works now.
Thanks for the help Mark

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

추가 답변 (1개)

Mark Whirdy
Mark Whirdy 2012년 12월 16일

1 개 추천

yearCellArray = {'2008';'2009';'2010'};
yearVector = cellfun(@str2double,yearCellArray);

댓글 수: 3

Thanks Mark works great,
however i have run into a strange problem, while it works for the years i have another cell array it does not seem to work for.
MWDIData_pop(i,1:5) = [3752373] [3876616] [4006236] [4140773] [4279524]
It must be something to do with the brackets, the previous vector was using ' symbols. But when i ask for class on any of the above numbers it still says it is a cell. I feel like i am probably missing something fundamental to Matlab here :)
Also Thanks
just
y = {'2008';'2009';'2010'};
str2double(y);
DoVile Last Name:
DoVile Last Name: 2012년 12월 16일
It doesnt work, it just returns NA values when i do it like that..

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

카테고리

도움말 센터File 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