Reading in excel file with multiple sheets into a cell array using readtable

4 vues (au cours des 30 derniers jours)
Hi,
I am trying to read in an excel file with multiple sheets (5 sheets) using the readtable command. Each of these sheets is 40x50. Here is the part of the code i wrote:
n=5; %no. of excel sheets
opts=detectImportOptions(the_excel_file);
opts=setvartype(opts,'char');
excel_data=cell(n,1) %initialising the cell array
for v=1:n
excel_data(v,1)=table2cell(readtable(num2str(the_excel_file),opts,'Sheet',num2str(excel_sheets(v,1)))); %excel sheets is a 5x1 string of the excel sheet names.
end
However, I get this error:
Unable to perform assignment because the size of the left side is 1-by-1 and the size of
the right side is 40x50.
I don't understand what I did wrong? I have tried many things but the same error occurs. I am trying to store the data in each of the 5 sheets into a 5x1 cell array, such that each cell contains all the data from one sheet.
Thank you

Réponse acceptée

Kevin Holly
Kevin Holly le 21 Juin 2022
Modifié(e) : Kevin Holly le 21 Juin 2022
You need to use curly brackets. Please see below.
n=5; %no. of excel sheets
excel_data=cell(n,1) %initialising the cell array
excel_data = 5×1 cell array
{0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
size(excel_data)
ans = 1×2
5 1
excel_data(1,1) % Notice this is a 1x1 cell array
ans = 1×1 cell array
{0×0 double}
excel_data{1,1} % to access the content inside the cell, use curly brackets.
ans = []
for v=1:n
data = rand(40,50);
excel_data{v,1}= data % use curly brackets here
end
excel_data = 5×1 cell array
{40×50 double} { 0×0 double} { 0×0 double} { 0×0 double} { 0×0 double}
excel_data = 5×1 cell array
{40×50 double} {40×50 double} { 0×0 double} { 0×0 double} { 0×0 double}
excel_data = 5×1 cell array
{40×50 double} {40×50 double} {40×50 double} { 0×0 double} { 0×0 double}
excel_data = 5×1 cell array
{40×50 double} {40×50 double} {40×50 double} {40×50 double} { 0×0 double}
excel_data = 5×1 cell array
{40×50 double} {40×50 double} {40×50 double} {40×50 double} {40×50 double}
  2 commentaires
Davindra Usov
Davindra Usov le 21 Juin 2022
Thank you so much! it worked a charm.
Davindra Usov
Davindra Usov le 21 Juin 2022
I want to locate the indices of the word 'Latitudes' in each of the cells in the array, but my method outputs an error.
Method:
indices = find(~cellfun('Latitudes', excel_data)); %excel data is the cell array
Error:
Error using cellfun
Unknown option.
When I google this I don't get much information. Do you know what I have done wrong?

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by