I'm using str2double but after conversion this gives me some values as 'NAN'. How to resolve this?
MATLAB: I have a cell array of size 136 by 1 and i want it to convert it to array of type double. How can i do this
cellcell arraysconvert
Related Solutions
33 of the cells in the third column contain empty arrays:
>> find(cellfun('isempty',cellStruct(:,3)))ans = 21968 21969 27848 35808 43424 52648 52649 52650 52651 52652 52653 52654 52655 60284 60285 60286 60287 60288 60289 60290 60291 60292 60293 60294 60295 60296 60297 60298 60299 65762 65763 65839 65840
While the rest of the cells in the third column apparently have one row:
>> find(cellfun('size',cellStruct(:,3),1)>1)ans = []
In any case, the total number of rows in all of the cells in the third colum is:
>> sum(cellfun('size',cellStruct(:,3),1))ans = 116364
And that is the exact number of rows that your numeric array contains:
>> size(doubArray,1)ans = 116364
So far nothing suggests that any data has gone "missing" when concatenating those cell contents.
Hi,
maybe consider using
>>uiimport
and let MATLAB do the job for you. You can generate M-Code for reading in the data, e.g.
%%Import data from text file.
% Script for importing data from the following text file:
%
% C:\Users\fhempel\Desktop\tmp\01012013.CSV
%% To extend the code to different selected data or a different text file,
% generate a function instead of a script.
% Auto-generated by MATLAB on 2014/04/22 11:37:26
%%Initialize variables.
filename = 'C:\Users\fhempel\Desktop\tmp\01012013.CSV';delimiter = ',';%%Read columns of data as strings:
% For more information, see the TEXTSCAN documentation.
formatSpec = '%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%[^\n\r]';%%Open the text file.
fileID = fopen(filename,'r');%%Read columns of data according to format string.
% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'MultipleDelimsAsOne', true, 'ReturnOnError', false);%%Close the text file.
fclose(fileID);%%Convert the contents of columns containing numeric strings to numbers.
% Replace non-numeric strings with NaN.
raw = repmat({''},length(dataArray{1}),length(dataArray)-1);for col=1:length(dataArray)-1 raw(1:length(dataArray{col}),col) = dataArray{col};endnumericData = NaN(size(dataArray{1},1),size(dataArray,2));for col=[3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,22,23,27] % Converts strings in the input cell array to numbers. Replaced non-numeric
% strings with NaN.
rawData = dataArray{col}; for row=1:size(rawData, 1); % Create a regular expression to detect and remove non-numeric prefixes and
% suffixes.
regexstr = '(?<prefix>.*?)(?<numbers>([-]*(\d+[\,]*)+[\.]{0,1}\d*[eEdD]{0,1}[-+]*\d*[i]{0,1})|([-]*(\d+[\,]*)*[\.]{1,1}\d+[eEdD]{0,1}[-+]*\d*[i]{0,1}))(?<suffix>.*)'; try result = regexp(rawData{row}, regexstr, 'names'); numbers = result.numbers; % Detected commas in non-thousand locations.
invalidThousandsSeparator = false; if any(numbers==','); thousandsRegExp = '^\d+?(\,\d{3})*\.{0,1}\d*$'; if isempty(regexp(thousandsRegExp, ',', 'once')); numbers = NaN; invalidThousandsSeparator = true; end end % Convert numeric strings to numbers.
if ~invalidThousandsSeparator; numbers = textscan(strrep(numbers, ',', ''), '%f'); numericData(row, col) = numbers{1}; raw{row, col} = numbers{1}; end catch me end end end%%Split data into numeric and cell columns.
rawNumericColumns = raw(:, [3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,22,23,27]);rawCellColumns = raw(:, [1,2,19,20,21,24,25,26]);%%Replace non-numeric cells with NaN
R = cellfun(@(x) ~isnumeric(x) && ~islogical(x),rawNumericColumns); % Find non-numeric cells
rawNumericColumns(R) = {NaN}; % Replace non-numeric cells
%%Allocate imported array to column variable names
DATE = rawCellColumns(:, 1);TIME = rawCellColumns(:, 2);TEMP = cell2mat(rawNumericColumns(:, 1));RH = cell2mat(rawNumericColumns(:, 2));WDIR = cell2mat(rawNumericColumns(:, 3));WSPEED = cell2mat(rawNumericColumns(:, 4));RAINFALL = cell2mat(rawNumericColumns(:, 5));APRESSURE = cell2mat(rawNumericColumns(:, 6));SRAD = cell2mat(rawNumericColumns(:, 7));EVAP = cell2mat(rawNumericColumns(:, 8));AN1 = cell2mat(rawNumericColumns(:, 9));AN2 = cell2mat(rawNumericColumns(:, 10));AN3 = cell2mat(rawNumericColumns(:, 11));AN4 = cell2mat(rawNumericColumns(:, 12));DIG1 = cell2mat(rawNumericColumns(:, 13));DIG2 = cell2mat(rawNumericColumns(:, 14));DIG3 = cell2mat(rawNumericColumns(:, 15));DIG4 = cell2mat(rawNumericColumns(:, 16));PV = rawCellColumns(:, 3);BAT = rawCellColumns(:, 4);ACV = rawCellColumns(:, 5);LAT = cell2mat(rawNumericColumns(:, 17));LON = cell2mat(rawNumericColumns(:, 18));ALTI = rawCellColumns(:, 6);ELEVATION = rawCellColumns(:, 7);DEVICEINFO = rawCellColumns(:, 8);CHECKSUM = cell2mat(rawNumericColumns(:, 19));%%Clear temporary variables
clearvars filename delimiter formatSpec fileID dataArray ans raw col numericData rawData row regexstr result numbers invalidThousandsSeparator thousandsRegExp me rawNumericColumns rawCellColumns R;
Best Answer