I have trouble with cell arrays

11 ビュー (過去 30 日間)
David
David 2014 年 10 月 9 日
編集済み: José-Luis 2014 年 10 月 9 日
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 日
res = C{:};
where C is your cell array would work in this case.
  5 件のコメント
Adam
Adam 2014 年 10 月 9 日
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 日
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 日
...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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by