Reading mix between numeric and non-numeric data from excel into Matlab - excel

I have a matrix where the first column contains dates and the first row contains maturities which are alpha/numeric (e.g. 16year).
The rest of the cells contain the rates for each day, which are double precision numbers.
Now I believe xlsread() can only handle numeric data so I think I will need something else or a combination of functions?
I would like to be able to read the table from excel into MATLAB as one array or perhaps a struct() so that I can keep all the data together.
The other problem is that some of the rates are given as '#N/A'. I want the cells where these values are stored to be kept but would like to change the value to blank=" ".
What is the best way to do this? Can it be done as part of the input process?

Well, from looking at matlab reference for xlsread you can use the format
[num,txt,raw] = xlsread(FILENAME)
and then you will have in num a matrix of your data, in txt the unreadable data, i.e. your text headers, and in raw you will have all of your data unprocessed. (including the text headers).
So I guess you could use the raw array, or a combination of the num and txt.
For your other problem, if your rates are 'pulled' from some other source, you can use
=IFERROR(RATE DATA,"")
and then there will be a blank instead of the error code #N\A.
Another solution (only for Windows) would be to use xlsread() format which allows running a function on your imported data,
[num,txt,raw,custom] = xlsread(filename,sheet,xlRange,'',functionHandler)
and let the function replace the NaN values with blank spots. (and you will have your output in the custom array)

Related

VBA - Export Sheet as CSV without rounding values in CSV [duplicate]

In the below code I am having problems making sure the file writer does not round my number off to a certain number of decimal places. I need to use a variant because sometimes the value is string and at other times it is a number.
How can I force it to write exactly what the variable is? For example the below code might show 0.00038 and I want to show the exact value.
Dim MyFile1 As Variant
Dim MyNumber as Variant
MyFile0 = "C:/myfile.csv"
fnum0 = FreeFile()
Open MyFile0 For Output As fnum0
MyNumber = 0.0003759656
Print #fnum0, MyNumber
Your code is fine. The issue is with excel. When opening a CSV in excel, including thru VBA, excel detirmines what a cell is. Typically, if it lloks like a number with more then 5 characters it will express it in Scientific notation or rounded to 5 places.
Note sure what you are doing with the CSV file before or where you are getting it from, but here are some options to prevent excel from changing your data:
In VBA use the Workbook.OpenText command to open the CSV with either:
-the all excel cells as text*
-the column stored number with so many decimal places
(note a max of 30 decimal places apply)
-a particular column store as text.
--A full list of option syntax here http://msdn.microsoft.com/en-us/library/office/bb22351(v=office.12).aspx
You may also import your CSV into an excel spreadsheet, it will give you the option to choose data type for each column. Then run the VBA against the excel file.
If you are not doing formulations in excel, I would recomend storing the number as a text string. VBA automatically converts strings of numbers into numeric values when needed.
It's likely that you're experiencing floating point errors. They are are very small errors that occur when numbers are converted from/to base 10 (human) to/from base 2 (computer). For your purposes, you need to determine what it means that two values are not equal. It's not just val1 <> val2 because that won't account for the tiny errors. If you're dealing with money, for instance, you probably don't care about anything less than a penny. So you might determine inequality as ABS(val1 - val2) > .001. You just need to determine what your tolerance for equality is and compare to that standard.
It may be that your number is not being stored as a number in vba, and when you write it Excel converts it. Say you're number is 1.789 and you write it to a cell that is formatted as 'GENERAL'. Excel will write 1.79 (and think that's the number, not even a formatting issue).
What I've discovered, is if you convert the number to a decimal first CDEC(YOURNUMBER), it will write it correctly. For good measure, I also verify that the cell is '.NUMBERFORMAT = "GENERAL'

Can you use excel formulas on report data?

So i have some rdfs that download from the server through rwservlet as excels (xls). My question is: can i use excel formulas like sum on the report returned data?
I tried formatting the data (i think here relies the problem as it seem it doesn't affect the data that's already there) and modifying the decimal separator to "." from the rdf. Neither work, not separate or together; the sum formula returns 0. However if i overwrite new data over report data in a field, sum works.
It seems to me it sees all the data returned from the report like text and the formatting doesn't affect the already there data.
Thank you
I go to the root of the problem: the format mask in the rdf was adding a space for sign for the positive numbers. Hence excel was not recognizing it as a number. Problem is after i put a "-" in the format mask to get rid of the space excel re-formats the number automatically from 1.00 to 1 for example. So basically the format mask in the rdf is overwritten, i could not use it at all and i'd get the same result.

How to read mixed string and number data from csv in matlab and manipulate

I'm looking to write a script for MATLAB that will import data from a csv file which has a first row containing string headers and the data in each of those columns is either string, date or numeric.
I want to then be able to filter the data in MATLAB according to instances of a particular string and number combination.
Any help appreciated!
Cheers!
I would recommend you to start with reading MATLAB documentation.
[num,txt,raw] = xlsread('myExample.xlsx')
Reads numeric, text and combined data, so, if your data is combined, then you need the cell array raw. After that, you do whatever you want with your cell array (Additional information is not provided since OP did not provide any specific information about the way the data would be filtered)
Try using readtable function in MATLAB.
It correctly imports csv file with header and mixed data type.
xlsread was imported by mixed csv file very incorrectly repeating the some rows while maintaining the same total rows.
I got this after searching for a long time:
MATLAB Central Question/Answer

Best way to import numeric and non-numeric data (string) from an excel file into MATLAB?

I want to know the best way of importing both number and non-numeric data (which is string in the present case) from an excel file into MATLAB? By best (or better) way, I mean all the data together in a variable (or data structure).
First, I tried uiopen(filename) function which opens a wizard and from there, I can import the data into a MATLAB variable. However, problem here is that it replaces all the non-numeric data with zeros which is not required. I later on, found that this function calls another function, named xlsread(filename), which is another way (actual way) of importing excel file.
Second (last) way that I tried (which seems to be better) is to use function called importdata(filename) which imports both numeric and non-numeric data into separate structure variables.
However, I am wondering if there exists some other way(s) to import everything into a single variable or data structure?
xlsread is the correct way to import data from Excel spreadsheets,both numeric and non-numeric data. Check the documentation:
[num,txt,raw] = xlsread(___) additionally returns the text fields in
cell array txt, and the unprocessed data (numbers and text) in cell
array raw using any of the input arguments in the previous syntaxes.
If xlRange is specified, leading blank rows and columns in the
worksheet that precede rows and columns with data are returned in raw.

VBA writing to file is rounding numeric values - how to prevent?

In the below code I am having problems making sure the file writer does not round my number off to a certain number of decimal places. I need to use a variant because sometimes the value is string and at other times it is a number.
How can I force it to write exactly what the variable is? For example the below code might show 0.00038 and I want to show the exact value.
Dim MyFile1 As Variant
Dim MyNumber as Variant
MyFile0 = "C:/myfile.csv"
fnum0 = FreeFile()
Open MyFile0 For Output As fnum0
MyNumber = 0.0003759656
Print #fnum0, MyNumber
Your code is fine. The issue is with excel. When opening a CSV in excel, including thru VBA, excel detirmines what a cell is. Typically, if it lloks like a number with more then 5 characters it will express it in Scientific notation or rounded to 5 places.
Note sure what you are doing with the CSV file before or where you are getting it from, but here are some options to prevent excel from changing your data:
In VBA use the Workbook.OpenText command to open the CSV with either:
-the all excel cells as text*
-the column stored number with so many decimal places
(note a max of 30 decimal places apply)
-a particular column store as text.
--A full list of option syntax here http://msdn.microsoft.com/en-us/library/office/bb22351(v=office.12).aspx
You may also import your CSV into an excel spreadsheet, it will give you the option to choose data type for each column. Then run the VBA against the excel file.
If you are not doing formulations in excel, I would recomend storing the number as a text string. VBA automatically converts strings of numbers into numeric values when needed.
It's likely that you're experiencing floating point errors. They are are very small errors that occur when numbers are converted from/to base 10 (human) to/from base 2 (computer). For your purposes, you need to determine what it means that two values are not equal. It's not just val1 <> val2 because that won't account for the tiny errors. If you're dealing with money, for instance, you probably don't care about anything less than a penny. So you might determine inequality as ABS(val1 - val2) > .001. You just need to determine what your tolerance for equality is and compare to that standard.
It may be that your number is not being stored as a number in vba, and when you write it Excel converts it. Say you're number is 1.789 and you write it to a cell that is formatted as 'GENERAL'. Excel will write 1.79 (and think that's the number, not even a formatting issue).
What I've discovered, is if you convert the number to a decimal first CDEC(YOURNUMBER), it will write it correctly. For good measure, I also verify that the cell is '.NUMBERFORMAT = "GENERAL'

Resources