필터 지우기
필터 지우기

Display row from Excel file in Matlab

조회 수: 3 (최근 30일)
Javier
Javier 2020년 7월 18일
댓글: Javier 2020년 7월 19일
Hello there, I am trying to generate a random number (n) to then look for the data in the row n in a Excel file.
When I used
X = readtable('file.xlsx')
instead of
X = importdata('file.xlsx')
the program worked fine, but I want to know how to make it work with the function importdata as well.
It gives me this error Index in position 1 exceeds array bounds (must not exceed 1).
This is the code I used:
X = importdata('file.xlsx')
n = input('Number of participants: ');
r = randi(n)
disp('The winner is: '+string(r))
disp('The winner data is: ')
disp(X(r,:))
  댓글 수: 2
Cris LaPierre
Cris LaPierre 2020년 7월 18일
Without knowing more about your file, we can't offer much help. The error message would suggest X only contains a single row. Check that X exists in your Workspace, and is the expected size.
Javier
Javier 2020년 7월 19일
It has 2 columns and 5 rows. In the first column are the numbers 1 to 5 and in the second column are names.

댓글을 달려면 로그인하십시오.

채택된 답변

Cris LaPierre
Cris LaPierre 2020년 7월 19일
편집: Cris LaPierre 2020년 7월 19일
The error message you are seeing is because importdata returns the results in a stucture. Numeric data would be in X.data, text data in X.textdata. Also from the documentation:
  • For ASCII files and spreadsheets, importdata expects to find numeric data in a rectangular form (that is, like a matrix).
Tables allow you to mix data types in the same row. Here, you will need to build the disp command by combining the two separate structure fields. Perhaps something like this:
disp([X.data(r) X.textdata(r)])
  댓글 수: 1
Javier
Javier 2020년 7월 19일
It worked perfectly. Thank you so much.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by