convert the cell array to double

2 visualizaciones (últimos 30 días)
sarfudeen
sarfudeen el 27 de Oct. de 2014
Editada: sarfudeen el 28 de Oct. de 2014
Hi...
I have a cell array of size 31685 X 59. I want convert into a double. I used str2double but it takes longer duration of 130 sec for execution of the conversion.Please suggest me a alternative way to make it faster to lesser than 5 sec.
The first two columns in the cell array are of Date and Time.
Awaiting for earlier response...
Regards, M. Sarfudeen
  2 comentarios
Jan
Jan el 27 de Oct. de 2014
Please post a minimal example of your data. "Date and Time" could be a string, a serial date number or two diffent columns. What is the wanted output?
Please post the code your are currently using. Then it is much easier to improve the code. Perhaps you only forgot to pre-allocate the output and you can solve this in milliseconds.
sarfudeen
sarfudeen el 28 de Oct. de 2014
Editada: sarfudeen el 28 de Oct. de 2014
Hi Jan,
My code as follows:
[File,Path] = uigetfile({'*.csv','MultiSelect','ON');
fmt = '%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q%q';
z = ischar(File);
if z == 0
num = length(File)
temp = cell(1,num);
parfor i = 1: num
fid1 = fopen(fullfile(Path,File{1,i}),'r');
temp(i) = textscan(fid1,fmt,'Delimiter',',','CollectOutput',1);
fclose(fid1);
end
end
This is my code for reading the multiple csv files.
The temp stores the data as cell array of strings. I would like to convert the temp to double using str2double but it takes too much of time (approx... 130 sec) as my files are of 365 files.
The first column is of Date with format as 'yyyy-mm-dd'
The second column is of Time with format as 'HH:MM:SS'
Kindly please provide me solution which makes the conversion as fast as possible at the earliest.
I am using Matlab R2012b version.

Iniciar sesión para comentar.

Respuestas (2)

Orion
Orion el 27 de Oct. de 2014
Hi,
if your cell array contains only doubles, then use cell2mat.
C = {1 2 ; 3 4};
M = cell2mat(C);

sarfudeen
sarfudeen el 27 de Oct. de 2014
I used A = cell2mat(CellArray) but the error message is of:
Cannot support cell arrays containing cell arrays or objects.
Error in repowerver1/sm1Callback (line 537) A = double(cell2mat(tempdata));
Error while evaluating uimenu Callback
The data format of cell array is of string

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by