cell2table is not recognizing certain columns (not all) containing double data
1 view (last 30 days)
Show older comments
I am trying to write a table to create a .csv file. I did the following when I faced this issue.
data = readcell('S.csv'); %Attached below with this Question
b = cell2table(data(:,3:5)); %Want these columns of the input data to be written to another file
% First column in "b" just contained empty cells
writetable(b,'data.csv');%Look into attachments
If anybody does have an idea about what's wrong here, kindly share with me about the workaround tricks to do this.
Thanks
2 Comments
the cyclist
on 16 May 2021
Is there a reason you did not do the simpler:
data = readtable('S.csv');
writetable(data(:,3:5),'data.csv');
Answers (1)
Star Strider
on 16 May 2021
I am not certain what the problem is with readcell, however unless it is absolutely necessary to use it, use readtable instead, with corresponding changes in creating ‘b’.
data = readcell('https://www.mathworks.com/matlabcentral/answers/uploaded_files/619743/S.csv') %Attached below with this Question
b = cell2table(data(:,3:5)) %Want these columns of the input data to be written to another file
data2 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/619743/S.csv') %Attached below with this Question
b2 = data2(:,3:5)
.
2 Comments
Star Strider
on 17 May 2021
One way is to use 'VariableNamingRule','preserve', for example with this file —
data2 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/619743/S.csv','VariableNamingRule','preserve')
However, in this file, there are duplicate ‘s1’ names, and since duplicate variable names are not permitted, they will be renamed, regardless of the 'VariableNamingRule' setting.
One option I did not think of yesterday that solves this problem —
data = readcell('https://www.mathworks.com/matlabcentral/answers/uploaded_files/619743/S.csv') %Attached below with this Question
b = cell2table(data(2:end,3:5), 'VariableNames',data(1,3:5))
It also creates a useful table. The code is very slightly more complicated, however it has the virtue of producing the result you want. I have no idea why the original version of the code failed, only that this one works.
.
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!