Readmartix function removes first blank row
27 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
When I'm using the readmartix function to import data from an excel sheet if the first row is blank or a set of NaNs the output matrix removes that first row. I would like to keep that row to keep matrix size consistent across data sets. Is there a option that can be easily changed to keep that row rather than it being removed then having to add it back later.
5 commentaires
dpb
le 5 Avr 2020
" I'm specifically calling on a specific sheet and range ie readmatrix('file name', 'sheet', 'sheetname', 'range', "C5:E20") Which should return a 15x3 matrix,..."
> numel(5:20)
ans =
16
>>
Maybe there's some confusion regarding what you expect as well, here...
Réponses (2)
David Hill
le 4 Avr 2020
opts = detectImportOptions('Data.csv');%look at opts.DataLines and change it
opts.DataLines=[1 Inf];
data=readmatrix('Data.csv',opts);
1 commentaire
dpb
le 5 Avr 2020
Yeah, what I was suggesting above altho for the specific case OP is actually using specific sheet:range so must be dealing with spreadsheet file...
dpb
le 5 Avr 2020
Well, let's quit hypothesizing and just see what happens...
% BB-M.xlsx has reshape(3:8,2,[]) at C2 upper left corner
>> n=xlsread('BB-M.xlsx',1,'C1:E3') % as we know,xlsread() left to own devices returns only data
n =
3 4 5
6 7 8
>> [n,~,r]=xlsread('BB-M.xlsx',1,'C1:E3') % the raw optional output it returns whole thing but cell array
n =
3 4 5
6 7 8
r =
3×3 cell array
{[NaN]} {[NaN]} {[NaN]}
{[ 3]} {[ 4]} {[ 5]}
{[ 6]} {[ 7]} {[ 8]}
>> opt=detectImportOptions('BB-M.xlsx') % see what the import option object is by default...
opt =
SpreadsheetImportOptions with properties:
Sheet Properties:
Sheet: ''
Replacement Properties:
MissingRule: 'fill'
ImportErrorRule: 'fill'
Variable Import Properties: Set types by name using setvartype
VariableNames: {'Var1', 'Var2', 'Var3'}
VariableTypes: {'double', 'double', 'double'}
SelectedVariableNames: {'Var1', 'Var2', 'Var3'}
VariableOptions: Show all 3 VariableOptions
Access VariableOptions sub-properties using setvaropts/getvaropts
PreserveVariableNames: false
Range Properties:
DataRange: 'C2' (Start Cell)
VariableNamesRange: ''
RowNamesRange: ''
VariableUnitsRange: ''
VariableDescriptionsRange: ''
To display a preview of the table, use preview
>> opt.DataRange='C1'; % OK it starts at data location it found, let's set the target specifically
>> readmatrix('BB-M.xlsx',opt) % and pass the modified object
ans =
NaN NaN NaN
3 4 5
6 7 8
>>
And, voila! The desired starting point is honored and missing data indicatiors returned.
Easy enough solution -- create an import options object to match what you want and pass it...you can have just one of these saved; no need to regenerate every time as long as the location and spreadsheet structure is the same.
Voir également
Catégories
En savoir plus sur Spreadsheets dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!