readmatrix collapses blanks/NaNs, but I want to keep those empty cells
56 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Voss
2024-10-29,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 个评论
Voss
2024-10-29,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
2024-10-29,16:01
移动:Voss
2024-10-29,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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Large Files and Big Data 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!