How to create a cell array of different data type?

1 vue (au cours des 30 derniers jours)
Joseph
Joseph le 24 Fév 2013
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?

Réponse acceptée

per isakson
per isakson le 24 Fév 2013
Modifié(e) : per isakson le 24 Fév 2013
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};

Plus de réponses (0)

Catégories

En savoir plus sur Cell Arrays dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by