Character to numeric - importing/conversion

11 ビュー (過去 30 日間)
Emily Howes
Emily Howes 2021 年 7 月 13 日
コメント済み: Emily Howes 2021 年 7 月 14 日
My numerical data keeps importing from MS Excel as 'char' type. I set the target type as number when I was importing it so I'm not sure why. I would like the data to be a column vector of numeric data. I've tried the str2num and str2double functions but I receive the below error message.
>> class SoilMoisture
ans=
'char'
>> SM=str2num(SoilMoisture)
Error using str2num (line 35)
Input must be a character vector or string scalar.
Please could you help me find a way to either import my data as numeric type or if not, convert it once it's imported? Thank you.
  12 件のコメント
Stephen23
Stephen23 2021 年 7 月 13 日
編集済み: Stephen23 2021 年 7 月 13 日
This command syntax
class 'T'
is exactly equivalent to this function syntax:
class('T')
which is very unlikely to be useful for you. Understanding the difference between command syntax and function syntax is critical to using MATLAB, which is why I explained this in an earlier comment and also linked to the documentation. In general I recommend avoiding command syntax, to avoid the problems you are having now.
Most likely you want to check the class of the variable T:
class(T)
which be class "table". You can use indexing or names to access the (numeric) data in a table:
Emily Howes
Emily Howes 2021 年 7 月 13 日
Brilliant, thank you for your help. I have just tried this and it is now giving the answer as 'table'.

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

採用された回答

Tanay Gupta
Tanay Gupta 2021 年 7 月 13 日
編集済み: Tanay Gupta 2021 年 7 月 13 日
The problem you are facing is most probably because you are importing the heading as well and trying to convert that into a number.
Spreadsheets often contain a mix of numeric and text data as well as variable and row names, which is best represented in MATLAB® as a table thats the reason the data is imported as a character. You can import data in numeric format by using the Import Tool or the readmatrix function.
To learn to use the import tool click here. While importing the data through the above tool select the output type as numeric matrix. This will automatically import the data in double format.
You can also use the readmatrix function which provides the same functionality in form of code. Click here to learn more about it.
Hope it helps.
  5 件のコメント
Tanay Gupta
Tanay Gupta 2021 年 7 月 14 日
Sending the data in two different tables. The land use table contains the data from land use column and the moisture_data contains data from other two columns. The moisture data is saved as double and the land use data is saved as a table. You can now load these data files into matlab and access each row using indexing.
Emily Howes
Emily Howes 2021 年 7 月 14 日
That's brilliant. Thank you @Tanay Gupta

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by