Hi,
I have a cell array and need to convert to array.
a = {[100, 200, 300, 400, 500], [1000, 2000, 3000, 4000, 5000], [1100,1200, 1300, 1400, 1500]};
for each set b = a(1);
I need array data like,
b = [100, 200, 300, 400, 500];
i tired cell to mat, its not working.

 采纳的回答

If you want to extract the content of one cell simply do:
b = a{1};
If you want to merge a couple of cells into an array or matrix you can do:
B2 = cell2mat(a([1 3]));
B3 = cell2mat(a([1,3])');
HTH

7 个评论

Hi Bjorn,
if i use b = a{1}; i get single cell as output i need as a array [100, 200, 300, 400, 500];
please suggest
Try my suggestion before re-asking for what my solution provides:
>> a = {[100, 200, 300, 400, 500], [1000, 2000, 3000, 4000, 5000], [1100,1200, 1300, 1400, 1500]}
a =
1x3 cell array
{1x5 double} {1x5 double} {1x5 double}
>> b = a{1}
b =
100 200 300 400 500
The values in b are now the contents of the first cell, which were identical to your requested array of [100, 200, 300, 400, 500].
If that doesn't solve your question you'll have to be more specific.
Hi,
I tried the way as shown above and its work,
but for the data i have i'm getting result as char array in a single cell.
thank you.
That, then, is because your data is not in the format you indicated. That must, and now you're forcing me to guess, be because the contents in cell 1 in your real array a is a string? If that is the case you have to investigate yourself, since we are not privy to that information. If that is the case you will have to convert the string to an array of numbers. This is a task completely different from extracting the information in a cell, but you can do something like:
b_string = '100 200 300 400 500';
b = str2num(b_string)
HTH
Hi Bjorn,
Thank you so much, its working.
earlier i was trying using str2mat it was not working.
Regards
chandan
You're welcome.
The reason was that you confused the two different indexings on cell-arrays, and then the data-format in the cell. When runing into these problems look at the data-type of the intermediate variables - that should reveal some of the reasons for the results or errors you get - for this I typically use (in your example):
whos b
That will tell the size and data-type of the variable b.
OK, thank you for good explanation.

请先登录,再进行评论。

更多回答(1 个)

KSSV
KSSV 2021-2-24
编辑:KSSV 2021-2-24
iwant = reshape(cell2mat(a),[],length(a))' ;
iwant(1,:)

3 个评论

Hi KSSV,
i'm getting data in a single cell, char array.
I need it as [100, 200, 300, 400, 500];
a = {[100, 200, 300, 400, 500], [1000, 2000, 3000, 4000, 5000], [1100,1200, 1300, 1400, 1500]};
iwant = reshape(cell2mat(a),[],length(a))' ;
iwant(1,:)
ans = 1×5
100 200 300 400 500
Hi,
Yeah i tired the way you suggested it works,
but for the data i have i'm getting result as char array in a single cell.
Thank you.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Cell Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by