Character to numeric - importing/conversion
Mostra commenti meno recenti
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
Stephen23
il 13 Lug 2021
@Emily Howes: please upload your data file by clicking the paperclip button.
Emily Howes
il 13 Lug 2021
Stephen23
il 13 Lug 2021
@Emily Howes: please upload your Excel file.
Emily Howes
il 13 Lug 2021
Modificato: Emily Howes
il 13 Lug 2021
Importing the numeric data as numeric works correctly for me:
T = readtable('SoilMoistData.xlsx')
Emily Howes
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?
Emily Howes
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:
- show the exact code that you are using (as text, no screenshots)
- show the output variable as displayed in the command window (as text, no screenshots)
- upload the file (if it is different to the one you uploaded earlier)
Emily Howes
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:
Emily Howes
il 13 Lug 2021
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Text Data Preparation in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!