How to create a cell array of different data type?

8 ビュー (過去 30 日間)
Joseph
Joseph 2013 年 2 月 24 日
I am trying to create a cell array of different data type to a single matrix
  • Take the first eleven columns. Some columns have decimals and characters.
  • Take all the rows of data and store them on one single matrix
I am proceding the following way,
clc,clear all
urlwrite('http://www.ndbc.noaa.gov/data/realtime2/41053.txt','SJ.txt'); % URL from CARICOOS
fid = fopen( 'SJ.txt', 'rt' );
DataCell = textscan(fid, '%d%d%d%d%d%d%f%f%s%s%s*[^\n]', 'HeaderLines', 2, 'CollectOutput', 1) ;
fclose(fid);
Data = cell2mat(DataCell);
MATLAB outputs this error,
??? Error using ==> cell2mat at 47
All contents of the input cell array must be of the same data type.
Error in ==> SJ_F at 11
Data = cell2mat(DataCell);
This is the way I want it stored
Data =
Columns 1 through 10
'2013' '02' '23' '23' '50' '110' '5.0' '7.0' 'MM' 'MM'
Column 11
'MM' % This is just one row of the data.
Could you please help?

採用された回答

per isakson
per isakson 2013 年 2 月 24 日
編集済み: per isakson 2013 年 2 月 24 日
Documentation says:
A = cell2mat(C) converts cell array C with contents of the **same** data type
into a single array, A.
Thus
Data = cell2mat(DataCell);
is not possible in your case since DataCell contains both numeric and character data.
However, DataCell as returned by textscan is close as is
cell_array_of_different_data_type = DataCell{1};

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by