Character to numeric - importing/conversion

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 Commenti

@Emily Howes: please upload your data file by clicking the paperclip button.
Sorry, do you mean the Excel file or the MatLab one?
@Emily Howes: please upload your Excel file.
Emily Howes
Emily Howes il 13 Lug 2021
Modificato: Emily Howes il 13 Lug 2021
Thanks, here it is...
I've been excluding the headings to import it.
Importing the numeric data as numeric works correctly for me:
T = readtable('SoilMoistData.xlsx')
T = 150×3 table
LandUse Plot SoilMoist _______ ____ _________ {'HG'} 1 12.9 {'HG'} 2 11.4 {'HG'} 3 11.6 {'HG'} 4 16.9 {'HG'} 5 11.3 {'HG'} 6 18.9 {'HG'} 7 12.7 {'HG'} 8 24.9 {'HG'} 9 9.3 {'HG'} 10 21 {'HG'} 11 13.4 {'HG'} 12 18.1 {'HG'} 13 12.9 {'HG'} 14 15.3 {'HG'} 15 15.7 {'HG'} 16 21.3
That's great. Thank you.
I have just tried the same code and have received the below error message. Do you know what may be causing this?
>> T=readtable('SoilMoistData.xlsx')
Error using readtable (line 245)
Unable to find or open 'SoilMoistData.xlsx'. Check the path and filename or file permissions.
Stephen23
Stephen23 il 13 Lug 2021
Modificato: Stephen23 il 13 Lug 2021
"Do you know what may be causing this?"
The error message states "Unable to find or open 'SoilMoistData.xlsx'". This means that the file you actually have is saved with a different name, or is saved in a different location to where you are telling MATLAB to look for it.
Did you try following the advice given in the error message? Is the file saved in MATLAB's current directory?
Ah, that's imported it now. Thank you, I didn't reaslise MatLab could only search that directory. It is still registering as 'char' type though. Could this now be converted with the str2num or str2double functions?
Thank you for your patience!
Stephen23
Stephen23 il 13 Lug 2021
Modificato: Stephen23 il 13 Lug 2021
"I didn't reaslise MatLab could only search that directory"
Actually MATLAB can import/export files from any folder on any drive that is accessible on your computer. Of course you will need to tell it the location, just like you would with any other programming language: see FULLFILE.
"It is still registering as 'char' type though. Could this now be converted with the str2num or str2double functions?"
Please do the following:
  1. show the exact code that you are using (as text, no screenshots)
  2. show the output variable as displayed in the command window (as text, no screenshots)
  3. upload the file (if it is different to the one you uploaded earlier)
It's the same data file as the one I uploaded earlier.
I've been using this code and getting the below answer:
>> T=readtable('SoilMoistureData.xlsx')
>> class 'T'
ans =
'char'
Stephen23
Stephen23 il 13 Lug 2021
Modificato: Stephen23 il 13 Lug 2021
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:
Brilliant, thank you for your help. I have just tried this and it is now giving the answer as 'table'.

Accedi per commentare.

 Risposta accettata

Tanay Gupta
Tanay Gupta il 13 Lug 2021
Modificato: Tanay Gupta il 13 Lug 2021
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 Commenti

Thank you @Tanay Gupta. I've tried importing with and without headings as tables and as vectors and get the 'char' type both times, although I do have one column that contains text. Would it be best to leave this out and import the rest as a numeric matrix?
I've now removed all text columns and headings and tried importing as tables, numeric matrices and vectors. All are still giving 'char' type data. Is there any way that I can convert this? I have tried the below although neither are yielding any results.
Thank you.
>> str2double SoilMoistData
ans =
NaN
>> str2num SoilMoistData
ans =
[]
Stephen23
Stephen23 il 13 Lug 2021
Modificato: Stephen23 il 13 Lug 2021
This command syntax:
str2double SoilMoistData
is exactly equivalent to this function syntax:
str2double('SoilMoistData')
which (because the character vector 'SoilMoistData' does not represent a valid number) is expected to return NaN.
Similarly for STR2NUM.
It looks like you are attempting to use STR2DOUBLE/STR2NUM to import file data. I cannot see anywhere in the STR2DOUBLE/STR2NUM documentation that either of them import any file data, or have anything to do with any files, or even anything to do with the OS.
The MATLAB documentation lists functions that can be used for importing/exporting file data:
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.
That's brilliant. Thank you @Tanay Gupta

Accedi per commentare.

Più risposte (0)

Categorie

Prodotti

Release

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by