Readmartix function removes first blank row

27 vues (au cours des 30 derniers jours)
Benjamin Binder-Markey
Benjamin Binder-Markey le 3 Avr 2020
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
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...
Benjamin Binder-Markey
Benjamin Binder-Markey le 6 Avr 2020
yes sorry late night tying just making up an example...

Connectez-vous pour commenter.

Réponses (2)

David Hill
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
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...

Connectez-vous pour commenter.


dpb
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.
  1 commentaire
Benjamin Binder-Markey
Benjamin Binder-Markey le 6 Avr 2020
Thanks for all the feedback! I found my error thanks to your solution, I was calling the option 'Range' vs. 'DataRange' as soon as I switched it to 'DataRange' it returned the correct size with the empty cells.
Thanks again.

Connectez-vous pour commenter.

Produits


Version

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by