how to choose a random excel cell in a certain coloum

조회 수: 3 (최근 30일)
Sofija Radulovic
Sofija Radulovic 2020년 6월 27일
댓글: Sindar 2020년 6월 27일
a=randi([1,7092]);
b=a+1;
dtname=['D','b'];
dt = xlsread('Final_NGA_Flatfile.xlsx','dtname':'dtname');
I want to import the data from the cell D__ where __ represents the number a+1. How do I do this? With my current code, the issue I am running into is "Error using xlsread (line 257)
Worksheet 'd' not found.
Error in New5StoryBuilding (line 44)
dt = xlsread('Final_NGA_Flatfile.xlsx','dtname':'dtname');"

답변 (1개)

Sindar
Sindar 2020년 6월 27일
편집: Sindar 2020년 6월 27일
If you have R2019a or above, use readmatrix.
But, the issue is that you are mixing up passing a variable and a string. It happens that you're code returns something slightly parseable:
>> 'dtname':'dtname'
'd'
but not what you expect. Try this:
a=randi([1,7092]);
b=a+1;
columnname="D";
dt = xlsread('Final_NGA_Flatfile.xlsx',columnname+b+":"+columnname+b);
% or
dt = readmatrix('Final_NGA_Flatfile.xlsx','Range',columnname+b+":"+columnname+b);
since
>> b=101;
>> columnname="D";
>> columnname+b+":"+columnname+b
"D101:D101"
  댓글 수: 1
Sindar
Sindar 2020년 6월 27일
note that just "D101" will probably not give you what you want, since Matlab is likely to interpret it as either the name of the sheet or as the first cell to read

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by