readmatrix collapses blanks/NaNs, but I want to keep those empty cells
55 views (last 30 days)
Show older comments
Attached are several .csv files of sample data. They're simple 11x3-ish matrices.
In Figure 1 there are several files: one is a "full" set, another is identical except some rows are deleted, a third has the deleted rows instead replaced with a character string, and the last is a single vector taken from one of the partial sets.
Figure 2 has the simple code used to import. It is simply as follows:
clear
clc
close all
opts = detectImportOptions('nonan.csv')
full=readmatrix('nonan.csv',opts)
partial=readmatrix('nans.csv',opts)
vectornan=readmatrix('vectornan.csv',opts)
nanpoop=readmatrix('nanpoop.csv',opts)
Figures 3 and 4 are the detectImportOptions and VariableImportOptions settings.
I want to import the nan'd set of data while maintaining the NaNs because the size of the vector/matrix is important. However, you can see in the workspace that the blank spaces are ignored and collapsed (the matrices are smaller) while the character vectors are correctly identified as "NaN". I realize that blanks are technically different than "NaN", but this used to never be a problem and now I'm running into this issue and correctly importing as part of the script is just not working and I can't figure it out. Also, there is no reason why "vectornan" should be importing as a matrix because I copy-pasted only one column; it should be a vector.
I CAN import manually using the import menu, and that DOES correctly identify blank spaces as "NaN". So I don't know why that works but the readmatrix isn't.
0 Comments
Accepted Answer
Voss
on 29 Oct 2024 at 15:17
Use 'EmptyLineRule','read' to keep the lines that are all-NaN. Using only that option, all sample files are read correctly:
full=readmatrix('nonan.csv','EmptyLineRule','read')
partial=readmatrix('nans.csv','EmptyLineRule','read')
vectornan=readmatrix('vectornan.csv','EmptyLineRule','read')
nanpoop=readmatrix('nanpoop.csv','EmptyLineRule','read')
Also, to address this:
"there is no reason why "vectornan" should be importing as a matrix because I copy-pasted only one column; it should be a vector"
The reason vectornan is a matrix with three columns is that you are calling readmatrix on vectornan.csv using the options detected from nonan.csv, which includes three variables, so the result of that readmatrix call includes three variables.
3 Comments
Voss
on 29 Oct 2024 at 15:43
"Do you know if there's a way to permanently and/or globally apply that rule so that I don't have to make that change to each line?"
I'm not aware of a way to set the readmatrix default options globally.
Star Strider
on 29 Oct 2024 at 16:01
Moved: Voss
on 29 Oct 2024 at 16:27
‘Do you know if there's a way to permanently and/or globally apply that rule so that I don't have to make that change to each line?’
Create your own function file (or anonymous function) that contains the required function call (readtable or readmatrix) and the desired name-value pair arguments, then call it with the file name and return the result (matrix or table) as the function output. Be careful to not name it the same as an existing MATLAB function.
More Answers (0)
See Also
Categories
Find more on Large Files and Big Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!