error while importing excel file

I am trying to import an excel file into matlab environment; however, excel file contains two sheets. I want to import only one sheet whichever user imports.
[filename,pathname,~]=uigetfile('.xlsx'); %select the excel file
sheets = sheetnames(filename);
[~, ~, raw] = xlsread([pathname filename],sheets);
Error using xlsread
Sheet argument must be a string scalar, a character vector, or an integer.
Error in importfile (line 4)
[~, ~, raw] = xlsread([pathname filename],sheets);

 採用された回答

Fangjun Jiang
Fangjun Jiang 2022 年 12 月 7 日

0 投票

Run the code line by line, check the value of some variables and you will find the problem easily.
[~, ~, raw] = xlsread([pathname filename],sheets);
should use fullfile(pathname,filename) to compose the full file name
sheets is a string array. You will probably need to use sheets(1)

3 件のコメント

AL
AL 2022 年 12 月 7 日
i have two different excel workbook, in which one contains only 1 sheet while otherone has 2 sheets.
while importing the workbench if i dont specify the sheet name it is giving me above error and if i mention sheet(1) in code then it is worrking for workbook which has only one sheet; however, for workbook 2. the main data is on sheet 2 and i want to import that sheet.
is there any way i can make it user define to enter whcih sheet he wants?
regards,
AL
Fangjun Jiang
Fangjun Jiang 2022 年 12 月 7 日
  1. Specify the sheet by number or name directly, e.g. xlsread(file, 2), xlsread(file,'sheet2')
  2. get the sheet names by using sheets=sheetnames(), and then compare "sheets" with the known name to find the index. This would be the most generic and robust approach.
AL
AL 2022 年 12 月 7 日
Thank you so much, now it is working.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeData Import from MATLAB についてさらに検索

製品

リリース

R2022b

質問済み:

AL
2022 年 12 月 7 日

コメント済み:

AL
2022 年 12 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by