readmatrix collapses blanks/NaNs, but I want to keep those empty cells
57 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
tyler chu
il 29 Ott 2024 alle 14:28
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 Commenti
Risposta accettata
Voss
il 29 Ott 2024 alle 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 Commenti
Voss
il 29 Ott 2024 alle 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
il 29 Ott 2024 alle 16:01
Spostato: Voss
il 29 Ott 2024 alle 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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Large Files and Big Data in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!