How to convert a .csv file of cell array type to double?

I have multiple .csv files of type Cell. I have attached 3 of these files for illustration and a data.csv file of type double. These files are located in path C:\Users\Anonymous\Documents\MATLAB
I want to load them one-by-one and convert them to type double and then to save them in the same path. I tried different ways, such as, cell2mat, str2mat, str2double, str2num, cell2csv (file exchange), etc. but without success. cell2mat gives error, and str2num convert everything in the file to NaN. Yes, it is true that my .csv files have characters like !, ., _, etc., but I still need to find a way to use the same file name and do the conversion to type double/numeric.

7 Comments

@hello_world: please upload some sample files by clicking the paperclip button. Currently we have no idea what they contain, which makes giving advice on how to import them almost impossible. Please upload some example files if you want help with this task.
"I have multiple .csv files of size N x 1 of type cell."
A file is how data is stored on hard-drive. A cell array is an object class in MATLAB memory. These are two totally unrelated concepts, so how can you have a file "of type cell" ? What do you mean by this?
@Stephen Cobeldick: I have uploaded csv files and modified my question.
File.A_label, File.B_label and File_C_label.csv you provided all contain strings, which do not contain numerical characters. How do you expect the conversion to be successful?
@Paolo I will like to concatenate given data and label files. For example, concat(data, File.A_label);. This is not possible because data is of type double while File.A_label is containing string. I thought of converting it to type double/numeric so that I can concatenate it with data.
And, even if I do not concatenate them, I still will like to have File.A_label.csv containing those string labels but of type numeric/double.
By the way, a similar question was answered by Jan and Daniel Shub at: https://www.mathworks.com/matlabcentral/answers/18509-cell-conversion-to-double, but in my case Jan's answer gives output [] and Daniel's answers gives output NaN.
As you mentioned here, the conversion is giving you NaN because those characters are not numerical, ie. the conversion is not possible.
Perhaps you wish to convert your doubles to string instead, and concatenate them with strings afterwards using with strcat?
Converting to double, categorical, etc, are all red herrings. The question states "I want to load them one-by-one and convert them to type double and then to save them in the same path". Remove the "convert to type double" and the task is simply one of merging file data. This does not require converting data types, or possibly even anything to do with numericals at all.
If hello_world explains the actual task that they are trying to achieve, then we could help them with that.
@Stephen Cobeldick My actual task is that I want to convert File.A_label.csv as given in my example to type double such that the converted file does not contain numeric entries, such as, 1s, 2s, 3s, etc.
Now, you are asking me why I want to do it? I think you ask this because once you know what is the next goal, you will switch to that problem rather than advising me how to do this conversion. I just want to see if this type of conversion is possible. I am not looking for help in post achieving type double conversion.
So I do not think that it is an XY problem as you have referenced. Walter Roberson's solution is almost close but not exactly what I want to achieve. Frankly speaking, I do not think that it is even possible.

Sign in to comment.

Answers (2)

Depending on what you are doing, you might want to use categorical(), or might simply want to use findgroups().
Note that several of the classification and neural network routines are happy to take a cell array of strings as labels.

7 Comments

@Walter Roberson: Can you please elaborate your answer?
[~, txt1] = xlsread('File.A_Label.csv');
[~, txt2] = xlsread('File.B_Label.csv');
[~, txt3] = xlsread('File.C_Label.csv');
then
catagories = categorical([txt1; txt2; txt3]); %data type would be "categorical"
or
group = findgroups([txt1; txt2; txt3]); %data type would be "double"
@Walter Roberson: categorical is ok, but it can not be concatenated with date type double, and if I convert categorical to type double using MATLAB's double function, it converts all entries to numeric 1. See data.csv file in my example.
findgroups converts all cell entries to numeric 1 while I will like to keep a string as such.
T1 = readtable('File.A_label.csv', 'delimiter', ',', 'readvar', false);
T2 = readtable('File.B_label.csv', 'delimiter', ',', 'readvar', false);
T3 = readtable('File_C_label.csv', 'delimiter', ',', 'readvar', false);
categories = catagorical([T1.Var1; T2.Var1; T3.Var1]);
catnums = double(categories);
catnums does not have only 1's for me.
"while I will like to keep a string as such."
If you need strings (or displayed as strings) mixed with double in a single array, then you need to use cell array or you need to use a table() or timetable()
What is it that you are trying to accomplish with this array if you had it?
@Walter Roberson: I dot want numeric output as 1s, 2s, 3s, etc. I want the output in string format, but type double because it is easy to manipulate, concatenate, plot, etc.
Your task is not possible. MATLAB has no possibility of having an item that is simultaneously a double and a string. The only computer language I can think of at the moment which permits that is AWK and its successors such as GAWK.

Sign in to comment.

Regarding hello_world's comment "str2num converts everything in the file to NaN": if you want to apply str2num or str2double to an entire table, it appears MATLAB expects each element or each variable to be specified. .
For example
t = table([1;2;3;4],{'5.1';'6.2';'7.3';'8.4'},{'9.1';'10.2';'11.3';'12.4'},{'13.1';'14.2';'15.3';'16.4'})
t.Var2 = str2double(t.Var2); % MATLAB converts a single column just fine
% Produces a NaN result
t2 = str2double(t(:, 2:4));
The following help page shows how to use varfun to specify variables or a for loop to specify elements.
Also note that cell2mat does not convert strings to numbers.

Categories

Asked:

on 4 Jul 2018

Edited:

on 27 Dec 2021

Community Treasure Hunt

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

Start Hunting!