How to convert a .csv file of cell array type to double?
조회 수: 36 (최근 30일)
이전 댓글 표시
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
Stephen23
2018년 7월 4일
편집: Stephen23
2018년 7월 4일
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.
답변 (2개)
Walter Roberson
2018년 7월 4일
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
Walter Roberson
2018년 7월 4일
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.
Maria Battle
2021년 12월 27일
편집: Maria Battle
2021년 12월 27일
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!