Can you call an Excel sheet just once?

2 visualizaciones (últimos 30 días)
Nivedita Tanksali
Nivedita Tanksali el 28 de Abr. de 2021
Comentada: dpb el 29 de Abr. de 2021
I'm writing a code that onvolves calling multiple columns of data from an excel sheet, but it's quite tedious to enter the name of the sheet every time like so:
T = xlsread('35_data.xlsx', 1, 'C1:C2000')
A = xlsread('35_data.xlsx', 1, 'A1:A2000') %There's around five columns like this
There's over thirty excel sheets, 35_data being one of them, but all of them are in the format number_data.
Is there any way I can just enter the name 35_data once at the beginning (having specified the sheet numbers for all required columns)
and have MATLAB extract all values (T, A....) pertaining to those columns?
  3 comentarios
Nivedita Tanksali
Nivedita Tanksali el 29 de Abr. de 2021
Do you perhaps have an example of how I could do this?
dpb
dpb el 29 de Abr. de 2021
Don't create named variables needlessly...
X = xlsread('35_data.xlsx', 1, 'A:C');
iTA=[1,3]; % define subset of variables wanted
TA=X(iTA);
...
and use the array TA(:,i) in lieu of T, A.
If you want you can break it down further to iT, iA as being 1, 3, respectively and then write
X(:,iT)
instead of T and not bother to decimate X.
Many choices, but when you write individual variables as the individual columns and read them individually, you kill all the advantages of MATLAB array indexing and vectorized operations as well as require opening and closing to read the input file that many times which is very inefficient and slow.
Truthfully, it would be better to just write
X=readmatrix(filename);
and then refer to the columns of X as desired; if X is very large, then it might be worthwhile to decimate it before going on to save some memory; otherwise it's probably not even needed to have done.
If there really is no need to bring in some columns at all, nvestigate detectImportOptions and the ability within it to name the specific columns/variables to be returned using readmatrix.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Data Import from MATLAB en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by