I have trouble with cell arrays

I currently have a 1x4 cell array where each of the four elements is a 134x1 array of different data types. The cell array was created from a text file by a textscan call. What I wanted is a 134x4 cell array. How do I get there?

댓글 수: 1

José-Luis
José-Luis 2014년 10월 9일
Well, a solution is not to use textscan() but a lower level routine like sscanf() that will allow you to specify formats for all elements.

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

답변 (2개)

Adam
Adam 2014년 10월 9일

1 개 추천

res = C{:};
where C is your cell array would work in this case.

댓글 수: 5

Sorry, but that doesnt work...
a = single(ones(4,1));
b = double(zeros(4,1));
c = {a b};
c2 = c{:};
c2 =
1
1
1
1
Adam
Adam 2014년 10월 9일
Ah, sorry, I missed the "different data types" part of the question and read it as you wanting a standard array.
res = [C{:}];
num2cell([c{:}])
I think is what is needed to get the cell array result.
David
David 2014년 10월 9일
That works for the simple example. My actuall cell contains character arrays too though. Is there a more universal way that can handle char and numerical data types?

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

Andrew Reibold
Andrew Reibold 2014년 10월 9일
편집: Andrew Reibold 2014년 10월 9일

0 개 추천

Is this what you are looking for?
a = {0 0 0 0};
b = {1 1 1 1};
c = {'1' '2' '3' '4'};
d = {'look,';'this';'has';'characters!'};
MyBigCell = {a{:}; b{:}; c{:}; d{:}} % <-- Try this with your cells
Output:
MyBigCell =
[ 0] [ 0] [ 0] [ 0]
[ 1] [ 1] [ 1] [ 1]
'1' '2' '3' '4'
'look,' 'this' 'has' 'characters!'

댓글 수: 10

David
David 2014년 10월 9일
편집: David 2014년 10월 9일
The cell contents are not cells themseleves, but arrays. I'll try and be more discriptive without giving away propriatary data...
a = single(ones(4,1));
b = double(zeros(4,1));
c = ['fast';'slow';'four';'five';];
d = true(4,1);
badcell = {a(:) b(:) c(:) d(:)}
small_example_of_cell_I_want_without_having_to_type_536_values_or_use_convoluted_for_loops = {1 0 'fast' true;
1 0 'slow' true;
1 0 'four' true;
1 0 'five' true;}
Output:
badcell =
[4x1 single] [4x1 double] [16x1 char] [4x1 logical]
small_example_of_cell_I_want_without_having_to_type_536_values_ =
[1] [0] 'fast' [1]
[1] [0] 'slow' [1]
[1] [0] 'four' [1]
[1] [0] 'five' [1]
Andrew Reibold
Andrew Reibold 2014년 10월 9일
편집: Andrew Reibold 2014년 10월 9일
Convert Character array to cell using cellstr.
Convert the logical array to cell using num2cell
If there is too many to do by hand, write loops to check class and do such :)
a = {0 0 0 0}
b = {1 1 1 1}
c = ['fast'; 'slow'; 'four'; 'five']
d = true(1,4)
c2 = cellstr(c)
c = c2'
d = num2cell(d)
MyCell = {a{:}; b{:}; c2{:}; d{:}}
MyCell =
[ 0] [ 0] [ 0] [ 0]
[ 1] [ 1] [ 1] [ 1]
'fast' 'slow' 'four' 'five'
[ 1] [ 1] [ 1] [ 1]
EDIT: Just noticed my final answer was transposed from what you wanted haha. Here
MyCell'=
[0] [1] 'fast' [1]
[0] [1] 'slow' [1]
[0] [1] 'four' [1]
[0] [1] 'five' [1]
David
David 2014년 10월 9일
편집: David 2014년 10월 9일
yea... thats aparent, but I'd rather not convert the arrays to cell arrays only to turn them back to differently organized arrays in a bigger cell array. I've got hundreds, soon to be thousands, of data sets to do this to. Otherwise I would just open this up in excell. I'm looking for something more low level and efficient.
edit:
some elaboration... unless I am missing something about indexing into cells, I would even have to extract the arrays from the cells, convert them into individual cells, and then recreate the cell I want.
Andrew Reibold
Andrew Reibold 2014년 10월 9일
편집: Andrew Reibold 2014년 10월 9일
The great part about scripting is that you really only have to do it one time no matter how many sets you have.
Good luck!
The bad part about inefficient scripting is that I leave the program running overnight and it's still not done in the morning. Matlab is suposed to be built to handle TONS of data efficiently. I don't want to sacrifice that because a human can't read:
badcell =
[4x1 single] [4x1 double] [16x1 char] [4x1 logical]
Andrew Reibold
Andrew Reibold 2014년 10월 9일
편집: Andrew Reibold 2014년 10월 9일
Last overnight and not be done?
I just ran a test case using larger arrays than yours (144 rather than 134) 1000 times in 30 seconds.
Is the problem related to a human's ability to read.. or is it really related to the ability to code? ;)
jk jk. I dont know how to help bud, I'm sorry. Thought I had a solution.
David
David 2014년 10월 9일
This is a one part of a big program. I've got to pull data off of the archive onto a local drive, import it into matlab, organize it into channels, search for anomolies, output it to something a human can read, and move on to the next file. Reguardless of how fast of a computer my IT department thinks is sufficient, I want to find an efficient way of doing this. The mathworks guy who trained me a few years ago stressed how inefficient changing types is, and that if you are using a for loop, there is probably a better way.
José-Luis
José-Luis 2014년 10월 9일
편집: José-Luis 2014년 10월 9일
Matlab is NOT built to handle tons of data efficiently. If you want to handle large amounts of data then you should use a database.
Processor speed is unlikely to be the bottleneck when handling large datasets. Available memory is more likely to be the culprit.
David
David 2014년 10월 9일
...perhaps I should have said process tons of data efficiently.
José-Luis
José-Luis 2014년 10월 9일
편집: José-Luis 2014년 10월 9일
Well, then you could start by not using textscan() but the faster sscanf().
Even faster would be to use a binary format to store your data instead of text. Of course, IO becomes more complicated then.
And if I'm going to be nitpicking, as much as I love Matlab, it is not the best tool to run things fast, but it's one of the better ones to write code fast.
JIT and whatnot, an overhead is an overhead.

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

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품

태그

질문:

2014년 10월 9일

편집:

2014년 10월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by