how to choose a random excel cell in a certain coloum

1 回表示 (過去 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 Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by