0

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
9
  • Is each element in C scalar or can there be vectors or arrays in an element? Commented Jan 27, 2016 at 23:30
  • What is the error you are getting? Commented Jan 27, 2016 at 23:31
  • @TimAdams: each element in C is either a scalar of type string or the element ';' of type string (the latter indicating a missing value and it should be replaced with NaN as is done with the function str2double). Commented Jan 27, 2016 at 23:38
  • @Lui The error i get: that EACH cell (i,j) from data(1:10,1:10) is replaced by A, a 10x10 array of doubles. Instead of data(1:10,1:10) is replaced by A. Commented Jan 27, 2016 at 23:42
  • @Paul try data(1:10,1:10) = A(:,:)? Commented Jan 28, 2016 at 0:11

1 Answer 1

0

It looks like you are trying to insert an array of numeric values into an array of cells. You need to convert one or the other so that they are the same type. For example, you could convert your data cell array to a numeric array (if it is numeric data):

cell2mat(data);
data(1:10,1:10) = A;

or convert the new data to a cell array:

data(1:10,1:10) = num2cell(A)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.