I imported data as a string (in cell arrays). Some columns contain numeric values and I want to put this numeric data in double arrays rather than cell arrays. I succeeded in this by using str2double(C) function with C being the 10x10 cell arrays converted into a 10x10 array of type double. Now I would like to replace the original 10x10 cell array (which is part of a larger 10x50 cell array called 'data') by the double array. The following does not seem to work:
A = str2double(C);
data(1:10,1:10) = A;
Instead of replacing the 10x10 cell array with the 10x10 double array, it replaced EACH cell (i,j) with the 10x10 double array. Now I can do it correctly with a double for loop looping over all row and columns cells(i,j) and replacing each element with the correct element from the double array A(i,j).
i=1:10
j=1:10
data(i,j)=A(i,j);
end
end
This seems a bit cumbersome and I think this can be done more efficiently. Any thoughts are welcome!
The data i import is a tsv file containing european aiport data:
function dat = tsv2cell(file)
% Read tsv file into cell array.
%
% Syntax:
% dat = tsv2cell(file)
%
% Input:
% file Name of tsv file, either as complete path, or in the current %folder.
%
% Output:
% dat Cell array containing the tsv data in rows and columns. Note
% that each cell contains string data only, numeric data is not
% made numeric. I.e. '3' is loaded as '3', not as 3.
% Open file
fid = fopen(file);
if fid==-1
error(['File ' file ' could not be found or opened.']);
end
% Read each line as string (we have to do this as the number of columns is unknown)
dat = textscan(fid, '%s', 'Delimiter', '');
% Close file
fclose(fid);
% Loop over lines and separate at tabs and commas
dat = dat{1};
tab = sprintf('\t');
for i = 1:size(dat,1)
str = dat{i,1};
% get indices in str where separators are (tabs and commas)
ind = [0 strfind(str, ',') strfind(str, tab) length(str)+1];
ind = sort(ind); % sort before looping
for j = 1:length(ind)-1
dat{i,j} = str(ind(j)+1:ind(j+1)-1);
end
end
Cscalar or can there be vectors or arrays in an element?data(1:10,1:10) = A(:,:)?