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

 採用された回答

Guillaume
Guillaume 2016 年 10 月 25 日

7 投票

yourtable.columnname = str2double(yourtable.columnname);
is all that is needed.

5 件のコメント

JFz
JFz 2016 年 10 月 25 日
Tried this but got the same error. Thanks.
Guillaume
Guillaume 2016 年 10 月 26 日
You're certainly not getting the same error at all and certainly not from str2double. str2double returns NaN on invalid input, it never errors.
So:
  1. What did you actually try?
  2. What is the exact error you got?
JFz
JFz 2016 年 10 月 27 日
str2double works!
Luisana Rodriguez Sequeira
Luisana Rodriguez Sequeira 2020 年 11 月 24 日
What can we do if str2double returns NaN?
Image Analyst
Image Analyst 2020 年 11 月 24 日
The the column is either nans or characters or something that is not a number. So don't try to cast it to doubles then!

サインインしてコメントする。

その他の回答 (3 件)

Chaya N
Chaya N 2016 年 10 月 25 日

0 投票

str2num('enter your string here');
Please note that this would only work for strings that contain numerical data

7 件のコメント

JFz
JFz 2016 年 10 月 25 日
Thanks. But my string is a table column: T.n. I keep getting error: Error using str2num (line 32) Requires string or character array input.
Chaya N
Chaya N 2016 年 10 月 25 日
編集済み: Chaya N 2016 年 10 月 25 日
If your table also has other columns with numbers in them, then you would most certainly get that error message. Try passing only that particular column into your str2num() function.
Also how is this table stored? Is it a cell array or a structure?
Guillaume
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
Chaya N 2016 年 10 月 25 日
@Guillaume: rmdir!
A bit of an extreme thing to do (even by accident), wouldn't you say?
Guillaume
Guillaume 2016 年 10 月 26 日
編集済み: Guillaume 2016 年 10 月 26 日
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
Chaya N 2016 年 10 月 26 日
I am learning new things here. Thank you.
JFz
JFz 2016 年 10 月 27 日
str2double works!
Thank you so much!

サインインしてコメントする。

Image Analyst
Image Analyst 2016 年 10 月 25 日

0 投票

You could use xlsread() instead of readtable().

1 件のコメント

JFz
JFz 2016 年 10 月 25 日
Will try it. Thanks.

サインインしてコメントする。

Priteshkumar Gohil
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
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
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
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 ExchangeData Type Conversion についてさらに検索

製品

タグ

質問済み:

JFz
2016 年 10 月 25 日

編集済み:

2021 年 7 月 8 日

Community Treasure Hunt

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

Start Hunting!

Translated by