How to replace a missing value of a table with a cell?

Hi all, I'm using command 'readtable' to import data from excel files. All the data read in are strings and matlab uses NaN to represent missing values.
Is there a way to assign a cell to a missing value? Because I want to do a table to array conversion, and the existing of NaN keeps giving me error like "Cannot concatenate the table variables 'Var40' and 'x3_1_3', because their types are double and cell".
Thank you!

4 commentaires

Jan
Jan le 13 Jan 2018
A short example of the inputs would be useful.
Yujian Wu
Yujian Wu le 13 Jan 2018
Modifié(e) : Yujian Wu le 13 Jan 2018
Sorry, I forgot to upload a picture. Shown in the figure, there's a missing value on the position of Var40. So when I tried to convert the table to an array, it gave me the red error.
Jan
Jan le 13 Jan 2018
It is difficult to use a screenshot by copy&paste to post some code to solve the problem. I have to spend the time to create such a table at first. It would be more convenient, if you post the code or the attach the data and the command to import it. This will increase the chance to get an answer.
Yujian Wu
Yujian Wu le 13 Jan 2018
Modifié(e) : Yujian Wu le 13 Jan 2018
Sorry, I was too eager back then. Data is attached in the csv file now. Below there is the code:
data = readtable('test.xlsx');
array = table2array(data);
After running the two lines of code, the error appeared.

Connectez-vous pour commenter.

 Réponse acceptée

per isakson
per isakson le 13 Jan 2018
Modifié(e) : per isakson le 14 Jan 2018
test.csv contains two rows
Features,,,,,,,,,,,,,,,,,,,,
F1,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,,18,19,20
That is not test.xlsx, which causes the problems you describe in your question. However,
data = readtable('test.csv');
array = table2array(data);
throws
Error using table2array (line 27)
Cannot concatenate the table variables 'Var2' and 'Var1', because their types are double and cell.
The table is
data =
Var1 Var2 Var3 Var4 ...
__________ ____ ____ ____
'Features' NaN NaN NaN ...
'F1' 1 2 3 ...
The first column is character (internally stored in a cell array, I assume) and the following are numerical.
>> data{2,2:end}
ans =
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 NaN 18 19 20
>>
What result do you want? See The XY-Problem
If you don't want a table, the function, readtable, might not be the best choice.

1 commentaire

Thank you per! The vector data{2, 2:end} is exactly what I want! Just as I said in the question, I planned to get an array first and then convert it to the vector. That's why I got stuck at the step of table2array. Now it's settled.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by