how to choose a random excel cell in a certain coloum

1 次查看(过去 30 天)
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

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by