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

조회 수: 1 (최근 30일)
Praveen N
Praveen N 2021년 5월 16일
댓글: Star Strider 2021년 5월 17일
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
the cyclist
the cyclist 2021년 5월 16일
Is there a reason you did not do the simpler:
data = readtable('S.csv');
writetable(data(:,3:5),'data.csv');
Praveen N
Praveen N 2021년 5월 17일
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 ?

댓글을 달려면 로그인하십시오.

답변 (1개)

Star Strider
Star Strider 2021년 5월 16일
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
Praveen N
Praveen N 2021년 5월 17일
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 2021년 5월 17일
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.
.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by