Question

I am new to MATLAB and was confused about how to obtain numeric arrays from cell arrays. According to MATLAB, I have some matrix result and I get the following:

>> size(result)
ans =
    1    15

>> result
ans =
    Columns 1 through 15
    [3]    [15]    [1x2 double]    [13] ... \\ Omitted for clarity

>> iscell(result)
ans =
    1

So it seems like result is a 2-D cell array of dimensions 1 x 15, but there is also a sub-array (?) within it, indicated by the [1x2 double]. In this particular example, let's assume that there is only one of these [1x2 double] elements. In other words, result consists of fourteen single-element columns and one multi-element column.

I checked the documentation and according to this page there is a method called cell2mat. Unfortunately it doesn't quite do what I need, because it outputs a 1x16 numeric array:

>> cell2mat(result)
ans =
    3    15    10    7    13 ... \\ Omitted for clarity

In this example, 10 and 7 were the elements that composed the [1x2 double], it's just that MATLAB for whatever reason didn't indicate that when printing the cell array.

Question: is there a way I can convert this matrix into a numeric array of arrays while preserving the grouping? Specifically, I'm hoping to get something of the form [[3], [15], [10, 7], [13], ...] and have these be numeric. Is this possible? For what it's worth, I'm using MATLAB with some Java code from matlabcontrol, and that software appears to require real-valued arrays-of-arrays.

Was it helpful?

Solution

There is no such thing as 'java-type' arrays of arrays in matlab. You can have matlab's own cell arrays of arrays and this is what you have right now. Also there is no 'direct' translation of cell arrays in matlab to java data structures (check here for more info on translating matlab data to java).

Quickly I can see three possible ways you could consider:

  1. Convert your results into string representation of array, pass this string to java, and then convert it back to what ever structure you want.
  2. Since cells go into java as array of objects (i.e. Object[]), you could work with these arrays in java to make them into what you want.
  3. You could also make java structures directly in matlab, i.e. before passing them into your java class/method. Thus, you could convert results cell into some java array (e.g. java array of arrays) or whatever. This is possible, because in matlab you can use java code and java data structures. Check here for more info on using java in matlab.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top