turn a table column into doubles
显示 更早的评论
Hi,
I have a Table that has a column that was read from an excel sheet. The column should be numbers but is currently shown as strings like '3.14', '2.22', .... How to turn this column into doubles?
Thanks.
jennifer
采纳的回答
更多回答(3 个)
Chaya N
2016-10-25
str2num('enter your string here');
Please note that this would only work for strings that contain numerical data
7 个评论
JFz
2016-10-25
Guillaume
2016-10-25
I recommend never using str2num and using str2double instead. The latter has two advantages:
- it accepts cell arrays of string as inputs (so you can give it a whole table column as well)
- it won't format your hard drive when passed the string 'rmdir(''C:\'', ''f'')' unlike str2num (Warning! don't try! str2num will happily execute any instruction in the string)
Chaya N
2016-10-25
@Guillaume: rmdir!
A bit of an extreme thing to do (even by accident), wouldn't you say?
Well, yes it's a bit extreme. The intent is to scare you.
The thing is: if you're using str2num to process any external input (from a prompt, a file, a webpage, etc.) you are asking for trouble. That input may be malicious, a prank, bad luck or invalid, str2num will not care and execute whatever instruction is in that string, with no warning.
Chaya N
2016-10-26
I am learning new things here. Thank you.
JFz
2016-10-27
Priteshkumar Gohil
2021-5-12
编辑:Priteshkumar Gohil
2021-5-12
0 个投票
e.g. if you want to convert 1st column then following should work.
yourtable.(1) = str2double(t.(1));
or
yourtable.('COLUMN_TITLE') = str2double(t.('COLUMN_TITLE'));
3 个评论
Arshey Dhangekar
2021-7-7
Hello I have csv data of 18 columns and want to convert into double
so instead of writing 18 str2double code how can I convert all the 18 columns in short line of code
Temp.x310_Ambient__C_ = str2double (Temp.x310_Ambient__C_);
Image Analyst
2021-7-8
@Arshey Dhangekar, start your own question and attach the csv file and code to import it into MATLAB, presumably with the readtable() function.
Arshey Dhangekar
2021-7-8
编辑:Arshey Dhangekar
2021-7-8
Hello I know basic thing. So only I want to know how can I convert all data into double. I attach csv file
Press=readtable ("inst0 138.221.155.184 12_10_2020 14_49_56 2.csv")
varfun(@class,Press,'OutputFormat','cell')
Output
Columns 1 through 8
{'double'} {'cell'} {'double'} {'double'} {'double'} {'double'} {'double'} {'double'}
Columns 9 through 16
{'double'} {'double'} {'double'} {'double'} {'double'} {'double'} {'double'} {'double'}
Columns 17 through 25
{'double'} {'double'} {'cell'} {'cell'} {'cell'} {'cell'} {'cell'} {'cell'} {'cell'}
Columns 26 through 29
{'cell'} {'cell'} {'cell'} {'double'}
类别
在 帮助中心 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!