cell2table is not recognizing certain columns (not all) containing double data

1 vue (au cours des 30 derniers jours)
Praveen N
Praveen N le 16 Mai 2021
Commenté : Star Strider le 17 Mai 2021
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 commentaires
the cyclist
the cyclist le 16 Mai 2021
Is there a reason you did not do the simpler:
data = readtable('S.csv');
writetable(data(:,3:5),'data.csv');
Praveen N
Praveen N le 17 Mai 2021
How can I avoid matlab renaming the column names ? I want them to be preserved as they were in original file. That is the reason why I am using readcell instead of readtable. readtable is detecting the first row as variable names and making them unique by modifying original column names. Any tricks to avoid it ?

Connectez-vous pour commenter.

Réponses (1)

Star Strider
Star Strider le 16 Mai 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
data = 1002×17 cell array
{'fm' } {'sm' } {'sm' } {'f' } {'s1' } {'s1' } {'s1' } {'fm' } {'sm' } {'sm' } {'sm' } {'sm' } {'f' } {'s1' } {'s1' } {'s1' } {'s1' } {[ 2]} {[ -3.8175]} {[ -3.8175]} {[ 2]} {[-1.3884]} {[-1.3884]} {[-1.3884]} {[ 2]} {[ -3.8175]} {[ -3.8175]} {[ -3.8175]} {[ -3.8175]} {[ 2]} {[-1.3884]} {[-1.3884]} {[-1.3884]} {[-1.3884]} {[2.0400]} {[ -4.1535]} {[ -4.1535]} {[2.0050]} {[-1.4146]} {[-1.4146]} {[-1.4146]} {[2.0400]} {[ -4.1535]} {[ -4.1535]} {[ -4.1535]} {[ -4.1535]} {[2.0050]} {[-1.4146]} {[-1.4146]} {[-1.4146]} {[-1.4146]} {[2.0800]} {[ -4.6785]} {[ -4.6785]} {[2.0100]} {[-1.4433]} {[-1.4433]} {[-1.4433]} {[2.0800]} {[ -4.6785]} {[ -4.6785]} {[ -4.6785]} {[ -4.6785]} {[2.0100]} {[-1.4433]} {[-1.4433]} {[-1.4433]} {[-1.4433]} {[2.1200]} {[ -5.7046]} {[ -5.7046]} {[2.0150]} {[-1.4746]} {[-1.4746]} {[-1.4746]} {[2.1200]} {[ -5.7046]} {[ -5.7046]} {[ -5.7046]} {[ -5.7046]} {[2.0150]} {[-1.4746]} {[-1.4746]} {[-1.4746]} {[-1.4746]} {[2.1600]} {[ -7.4639]} {[ -7.4639]} {[2.0200]} {[-1.5086]} {[-1.5086]} {[-1.5086]} {[2.1600]} {[ -7.4639]} {[ -7.4639]} {[ -7.4639]} {[ -7.4639]} {[2.0200]} {[-1.5086]} {[-1.5086]} {[-1.5086]} {[-1.5086]} {[2.2000]} {[-10.3309]} {[-10.3309]} {[2.0250]} {[-1.5454]} {[-1.5454]} {[-1.5454]} {[2.2000]} {[-10.3309]} {[-10.3309]} {[-10.3309]} {[-10.3309]} {[2.0250]} {[-1.5454]} {[-1.5454]} {[-1.5454]} {[-1.5454]} {[2.2400]} {[-13.7420]} {[-13.7420]} {[2.0300]} {[-1.5852]} {[-1.5852]} {[-1.5852]} {[2.2400]} {[-13.7420]} {[-13.7420]} {[-13.7420]} {[-13.7420]} {[2.0300]} {[-1.5852]} {[-1.5852]} {[-1.5852]} {[-1.5852]} {[2.2800]} {[-16.5223]} {[-16.5223]} {[2.0350]} {[-1.6281]} {[-1.6281]} {[-1.6281]} {[2.2800]} {[-16.5223]} {[-16.5223]} {[-16.5223]} {[-16.5223]} {[2.0350]} {[-1.6281]} {[-1.6281]} {[-1.6281]} {[-1.6281]} {[2.3200]} {[-17.6208]} {[-17.6208]} {[2.0400]} {[-1.6743]} {[-1.6743]} {[-1.6743]} {[2.3200]} {[-17.6208]} {[-17.6208]} {[-17.6208]} {[-17.6208]} {[2.0400]} {[-1.6743]} {[-1.6743]} {[-1.6743]} {[-1.6743]} {[2.3600]} {[-17.3200]} {[-17.3200]} {[2.0450]} {[-1.7238]} {[-1.7238]} {[-1.7238]} {[2.3600]} {[-17.3200]} {[-17.3200]} {[-17.3200]} {[-17.3200]} {[2.0450]} {[-1.7238]} {[-1.7238]} {[-1.7238]} {[-1.7238]} {[2.4000]} {[-14.9784]} {[-14.9784]} {[2.0500]} {[-1.7769]} {[-1.7769]} {[-1.7769]} {[2.4000]} {[-14.9784]} {[-14.9784]} {[-14.9784]} {[-14.9784]} {[2.0500]} {[-1.7769]} {[-1.7769]} {[-1.7769]} {[-1.7769]} {[2.4400]} {[-12.5270]} {[-12.5270]} {[2.0550]} {[-1.8337]} {[-1.8337]} {[-1.8337]} {[2.4400]} {[-12.5270]} {[-12.5270]} {[-12.5270]} {[-12.5270]} {[2.0550]} {[-1.8337]} {[-1.8337]} {[-1.8337]} {[-1.8337]} {[2.4800]} {[-10.0090]} {[-10.0090]} {[2.0600]} {[-1.8943]} {[-1.8943]} {[-1.8943]} {[2.4800]} {[-10.0090]} {[-10.0090]} {[-10.0090]} {[-10.0090]} {[2.0600]} {[-1.8943]} {[-1.8943]} {[-1.8943]} {[-1.8943]} {[2.5200]} {[ -7.9086]} {[ -7.9086]} {[2.0650]} {[-1.9589]} {[-1.9589]} {[-1.9589]} {[2.5200]} {[ -7.9086]} {[ -7.9086]} {[ -7.9086]} {[ -7.9086]} {[2.0650]} {[-1.9589]} {[-1.9589]} {[-1.9589]} {[-1.9589]} {[2.5600]} {[ -6.6341]} {[ -6.6341]} {[2.0700]} {[-2.0276]} {[-2.0276]} {[-2.0276]} {[2.5600]} {[ -6.6341]} {[ -6.6341]} {[ -6.6341]} {[ -6.6341]} {[2.0700]} {[-2.0276]} {[-2.0276]} {[-2.0276]} {[-2.0276]}
b = cell2table(data(:,3:5)) %Want these columns of the input data to be written to another file
b = 1002×3 table
Var1 Var2 Var3 ____________ ____ ___________ {'sm' } f {'s1' } {[ -3.8175]} {[-1.3884]} {[ -4.1535]} {[-1.4146]} {[ -4.6785]} {[-1.4433]} {[ -5.7046]} {[-1.4746]} {[ -7.4639]} {[-1.5086]} {[-10.3309]} {[-1.5454]} {[-13.7420]} {[-1.5852]} {[-16.5223]} {[-1.6281]} {[-17.6208]} {[-1.6743]} {[-17.3200]} {[-1.7238]} {[-14.9784]} {[-1.7769]} {[-12.5270]} {[-1.8337]} {[-10.0090]} {[-1.8943]} {[ -7.9086]} {[-1.9589]} {[ -6.6341]} {[-2.0276]}
data2 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/619743/S.csv') %Attached below with this Question
data2 = 1001×17 table
fm sm sm_1 f s1 s1_1 s1_2 fm_1 sm_2 sm_3 sm_4 sm_5 f_1 s1_3 s1_4 s1_5 s1_6 ____ _______ _______ _____ _______ _______ _______ ____ _______ _______ _______ _______ _____ _______ _______ _______ _______ 2 -3.8175 -3.8175 2 -1.3884 -1.3884 -1.3884 2 -3.8175 -3.8175 -3.8175 -3.8175 2 -1.3884 -1.3884 -1.3884 -1.3884 2.04 -4.1535 -4.1535 2.005 -1.4146 -1.4146 -1.4146 2.04 -4.1535 -4.1535 -4.1535 -4.1535 2.005 -1.4146 -1.4146 -1.4146 -1.4146 2.08 -4.6785 -4.6785 2.01 -1.4433 -1.4433 -1.4433 2.08 -4.6785 -4.6785 -4.6785 -4.6785 2.01 -1.4433 -1.4433 -1.4433 -1.4433 2.12 -5.7046 -5.7046 2.015 -1.4746 -1.4746 -1.4746 2.12 -5.7046 -5.7046 -5.7046 -5.7046 2.015 -1.4746 -1.4746 -1.4746 -1.4746 2.16 -7.4639 -7.4639 2.02 -1.5086 -1.5086 -1.5086 2.16 -7.4639 -7.4639 -7.4639 -7.4639 2.02 -1.5086 -1.5086 -1.5086 -1.5086 2.2 -10.331 -10.331 2.025 -1.5454 -1.5454 -1.5454 2.2 -10.331 -10.331 -10.331 -10.331 2.025 -1.5454 -1.5454 -1.5454 -1.5454 2.24 -13.742 -13.742 2.03 -1.5852 -1.5852 -1.5852 2.24 -13.742 -13.742 -13.742 -13.742 2.03 -1.5852 -1.5852 -1.5852 -1.5852 2.28 -16.522 -16.522 2.035 -1.6281 -1.6281 -1.6281 2.28 -16.522 -16.522 -16.522 -16.522 2.035 -1.6281 -1.6281 -1.6281 -1.6281 2.32 -17.621 -17.621 2.04 -1.6743 -1.6743 -1.6743 2.32 -17.621 -17.621 -17.621 -17.621 2.04 -1.6743 -1.6743 -1.6743 -1.6743 2.36 -17.32 -17.32 2.045 -1.7238 -1.7238 -1.7238 2.36 -17.32 -17.32 -17.32 -17.32 2.045 -1.7238 -1.7238 -1.7238 -1.7238 2.4 -14.978 -14.978 2.05 -1.7769 -1.7769 -1.7769 2.4 -14.978 -14.978 -14.978 -14.978 2.05 -1.7769 -1.7769 -1.7769 -1.7769 2.44 -12.527 -12.527 2.055 -1.8337 -1.8337 -1.8337 2.44 -12.527 -12.527 -12.527 -12.527 2.055 -1.8337 -1.8337 -1.8337 -1.8337 2.48 -10.009 -10.009 2.06 -1.8943 -1.8943 -1.8943 2.48 -10.009 -10.009 -10.009 -10.009 2.06 -1.8943 -1.8943 -1.8943 -1.8943 2.52 -7.9086 -7.9086 2.065 -1.9589 -1.9589 -1.9589 2.52 -7.9086 -7.9086 -7.9086 -7.9086 2.065 -1.9589 -1.9589 -1.9589 -1.9589 2.56 -6.6341 -6.6341 2.07 -2.0276 -2.0276 -2.0276 2.56 -6.6341 -6.6341 -6.6341 -6.6341 2.07 -2.0276 -2.0276 -2.0276 -2.0276 2.6 -5.6447 -5.6447 2.075 -2.1005 -2.1005 -2.1005 2.6 -5.6447 -5.6447 -5.6447 -5.6447 2.075 -2.1005 -2.1005 -2.1005 -2.1005
b2 = data2(:,3:5)
b2 = 1001×3 table
sm_1 f s1 _______ _____ _______ -3.8175 2 -1.3884 -4.1535 2.005 -1.4146 -4.6785 2.01 -1.4433 -5.7046 2.015 -1.4746 -7.4639 2.02 -1.5086 -10.331 2.025 -1.5454 -13.742 2.03 -1.5852 -16.522 2.035 -1.6281 -17.621 2.04 -1.6743 -17.32 2.045 -1.7238 -14.978 2.05 -1.7769 -12.527 2.055 -1.8337 -10.009 2.06 -1.8943 -7.9086 2.065 -1.9589 -6.6341 2.07 -2.0276 -5.6447 2.075 -2.1005
.
  2 commentaires
Praveen N
Praveen N le 17 Mai 2021
How can I avoid matlab renaming the column names ? I want them to be preserved as they were in original file. That is the reason why I am using readcell instead of readtable. readtable is detecting the first row as variable names and making them unique by modifying original column names. Any tricks to avoid it ?
Star Strider
Star Strider le 17 Mai 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')
data2 = 1001×17 table
fm sm sm_1 f s1 s1_1 s1_2 fm_1 sm_2 sm_3 sm_4 sm_5 f_1 s1_3 s1_4 s1_5 s1_6 ____ _______ _______ _____ _______ _______ _______ ____ _______ _______ _______ _______ _____ _______ _______ _______ _______ 2 -3.8175 -3.8175 2 -1.3884 -1.3884 -1.3884 2 -3.8175 -3.8175 -3.8175 -3.8175 2 -1.3884 -1.3884 -1.3884 -1.3884 2.04 -4.1535 -4.1535 2.005 -1.4146 -1.4146 -1.4146 2.04 -4.1535 -4.1535 -4.1535 -4.1535 2.005 -1.4146 -1.4146 -1.4146 -1.4146 2.08 -4.6785 -4.6785 2.01 -1.4433 -1.4433 -1.4433 2.08 -4.6785 -4.6785 -4.6785 -4.6785 2.01 -1.4433 -1.4433 -1.4433 -1.4433 2.12 -5.7046 -5.7046 2.015 -1.4746 -1.4746 -1.4746 2.12 -5.7046 -5.7046 -5.7046 -5.7046 2.015 -1.4746 -1.4746 -1.4746 -1.4746 2.16 -7.4639 -7.4639 2.02 -1.5086 -1.5086 -1.5086 2.16 -7.4639 -7.4639 -7.4639 -7.4639 2.02 -1.5086 -1.5086 -1.5086 -1.5086 2.2 -10.331 -10.331 2.025 -1.5454 -1.5454 -1.5454 2.2 -10.331 -10.331 -10.331 -10.331 2.025 -1.5454 -1.5454 -1.5454 -1.5454 2.24 -13.742 -13.742 2.03 -1.5852 -1.5852 -1.5852 2.24 -13.742 -13.742 -13.742 -13.742 2.03 -1.5852 -1.5852 -1.5852 -1.5852 2.28 -16.522 -16.522 2.035 -1.6281 -1.6281 -1.6281 2.28 -16.522 -16.522 -16.522 -16.522 2.035 -1.6281 -1.6281 -1.6281 -1.6281 2.32 -17.621 -17.621 2.04 -1.6743 -1.6743 -1.6743 2.32 -17.621 -17.621 -17.621 -17.621 2.04 -1.6743 -1.6743 -1.6743 -1.6743 2.36 -17.32 -17.32 2.045 -1.7238 -1.7238 -1.7238 2.36 -17.32 -17.32 -17.32 -17.32 2.045 -1.7238 -1.7238 -1.7238 -1.7238 2.4 -14.978 -14.978 2.05 -1.7769 -1.7769 -1.7769 2.4 -14.978 -14.978 -14.978 -14.978 2.05 -1.7769 -1.7769 -1.7769 -1.7769 2.44 -12.527 -12.527 2.055 -1.8337 -1.8337 -1.8337 2.44 -12.527 -12.527 -12.527 -12.527 2.055 -1.8337 -1.8337 -1.8337 -1.8337 2.48 -10.009 -10.009 2.06 -1.8943 -1.8943 -1.8943 2.48 -10.009 -10.009 -10.009 -10.009 2.06 -1.8943 -1.8943 -1.8943 -1.8943 2.52 -7.9086 -7.9086 2.065 -1.9589 -1.9589 -1.9589 2.52 -7.9086 -7.9086 -7.9086 -7.9086 2.065 -1.9589 -1.9589 -1.9589 -1.9589 2.56 -6.6341 -6.6341 2.07 -2.0276 -2.0276 -2.0276 2.56 -6.6341 -6.6341 -6.6341 -6.6341 2.07 -2.0276 -2.0276 -2.0276 -2.0276 2.6 -5.6447 -5.6447 2.075 -2.1005 -2.1005 -2.1005 2.6 -5.6447 -5.6447 -5.6447 -5.6447 2.075 -2.1005 -2.1005 -2.1005 -2.1005
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
data = 1002×17 cell array
{'fm' } {'sm' } {'sm' } {'f' } {'s1' } {'s1' } {'s1' } {'fm' } {'sm' } {'sm' } {'sm' } {'sm' } {'f' } {'s1' } {'s1' } {'s1' } {'s1' } {[ 2]} {[ -3.8175]} {[ -3.8175]} {[ 2]} {[-1.3884]} {[-1.3884]} {[-1.3884]} {[ 2]} {[ -3.8175]} {[ -3.8175]} {[ -3.8175]} {[ -3.8175]} {[ 2]} {[-1.3884]} {[-1.3884]} {[-1.3884]} {[-1.3884]} {[2.0400]} {[ -4.1535]} {[ -4.1535]} {[2.0050]} {[-1.4146]} {[-1.4146]} {[-1.4146]} {[2.0400]} {[ -4.1535]} {[ -4.1535]} {[ -4.1535]} {[ -4.1535]} {[2.0050]} {[-1.4146]} {[-1.4146]} {[-1.4146]} {[-1.4146]} {[2.0800]} {[ -4.6785]} {[ -4.6785]} {[2.0100]} {[-1.4433]} {[-1.4433]} {[-1.4433]} {[2.0800]} {[ -4.6785]} {[ -4.6785]} {[ -4.6785]} {[ -4.6785]} {[2.0100]} {[-1.4433]} {[-1.4433]} {[-1.4433]} {[-1.4433]} {[2.1200]} {[ -5.7046]} {[ -5.7046]} {[2.0150]} {[-1.4746]} {[-1.4746]} {[-1.4746]} {[2.1200]} {[ -5.7046]} {[ -5.7046]} {[ -5.7046]} {[ -5.7046]} {[2.0150]} {[-1.4746]} {[-1.4746]} {[-1.4746]} {[-1.4746]} {[2.1600]} {[ -7.4639]} {[ -7.4639]} {[2.0200]} {[-1.5086]} {[-1.5086]} {[-1.5086]} {[2.1600]} {[ -7.4639]} {[ -7.4639]} {[ -7.4639]} {[ -7.4639]} {[2.0200]} {[-1.5086]} {[-1.5086]} {[-1.5086]} {[-1.5086]} {[2.2000]} {[-10.3309]} {[-10.3309]} {[2.0250]} {[-1.5454]} {[-1.5454]} {[-1.5454]} {[2.2000]} {[-10.3309]} {[-10.3309]} {[-10.3309]} {[-10.3309]} {[2.0250]} {[-1.5454]} {[-1.5454]} {[-1.5454]} {[-1.5454]} {[2.2400]} {[-13.7420]} {[-13.7420]} {[2.0300]} {[-1.5852]} {[-1.5852]} {[-1.5852]} {[2.2400]} {[-13.7420]} {[-13.7420]} {[-13.7420]} {[-13.7420]} {[2.0300]} {[-1.5852]} {[-1.5852]} {[-1.5852]} {[-1.5852]} {[2.2800]} {[-16.5223]} {[-16.5223]} {[2.0350]} {[-1.6281]} {[-1.6281]} {[-1.6281]} {[2.2800]} {[-16.5223]} {[-16.5223]} {[-16.5223]} {[-16.5223]} {[2.0350]} {[-1.6281]} {[-1.6281]} {[-1.6281]} {[-1.6281]} {[2.3200]} {[-17.6208]} {[-17.6208]} {[2.0400]} {[-1.6743]} {[-1.6743]} {[-1.6743]} {[2.3200]} {[-17.6208]} {[-17.6208]} {[-17.6208]} {[-17.6208]} {[2.0400]} {[-1.6743]} {[-1.6743]} {[-1.6743]} {[-1.6743]} {[2.3600]} {[-17.3200]} {[-17.3200]} {[2.0450]} {[-1.7238]} {[-1.7238]} {[-1.7238]} {[2.3600]} {[-17.3200]} {[-17.3200]} {[-17.3200]} {[-17.3200]} {[2.0450]} {[-1.7238]} {[-1.7238]} {[-1.7238]} {[-1.7238]} {[2.4000]} {[-14.9784]} {[-14.9784]} {[2.0500]} {[-1.7769]} {[-1.7769]} {[-1.7769]} {[2.4000]} {[-14.9784]} {[-14.9784]} {[-14.9784]} {[-14.9784]} {[2.0500]} {[-1.7769]} {[-1.7769]} {[-1.7769]} {[-1.7769]} {[2.4400]} {[-12.5270]} {[-12.5270]} {[2.0550]} {[-1.8337]} {[-1.8337]} {[-1.8337]} {[2.4400]} {[-12.5270]} {[-12.5270]} {[-12.5270]} {[-12.5270]} {[2.0550]} {[-1.8337]} {[-1.8337]} {[-1.8337]} {[-1.8337]} {[2.4800]} {[-10.0090]} {[-10.0090]} {[2.0600]} {[-1.8943]} {[-1.8943]} {[-1.8943]} {[2.4800]} {[-10.0090]} {[-10.0090]} {[-10.0090]} {[-10.0090]} {[2.0600]} {[-1.8943]} {[-1.8943]} {[-1.8943]} {[-1.8943]} {[2.5200]} {[ -7.9086]} {[ -7.9086]} {[2.0650]} {[-1.9589]} {[-1.9589]} {[-1.9589]} {[2.5200]} {[ -7.9086]} {[ -7.9086]} {[ -7.9086]} {[ -7.9086]} {[2.0650]} {[-1.9589]} {[-1.9589]} {[-1.9589]} {[-1.9589]} {[2.5600]} {[ -6.6341]} {[ -6.6341]} {[2.0700]} {[-2.0276]} {[-2.0276]} {[-2.0276]} {[2.5600]} {[ -6.6341]} {[ -6.6341]} {[ -6.6341]} {[ -6.6341]} {[2.0700]} {[-2.0276]} {[-2.0276]} {[-2.0276]} {[-2.0276]}
b = cell2table(data(2:end,3:5), 'VariableNames',data(1,3:5))
b = 1001×3 table
sm f s1 _______ _____ _______ -3.8175 2 -1.3884 -4.1535 2.005 -1.4146 -4.6785 2.01 -1.4433 -5.7046 2.015 -1.4746 -7.4639 2.02 -1.5086 -10.331 2.025 -1.5454 -13.742 2.03 -1.5852 -16.522 2.035 -1.6281 -17.621 2.04 -1.6743 -17.32 2.045 -1.7238 -14.978 2.05 -1.7769 -12.527 2.055 -1.8337 -10.009 2.06 -1.8943 -7.9086 2.065 -1.9589 -6.6341 2.07 -2.0276 -5.6447 2.075 -2.1005
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.
.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by