MATLAB Answers

how to choose a random excel cell in a certain coloum

2 views (last 30 days)
Sofija Radulovic
Sofija Radulovic on 27 Jun 2020
Commented: Sindar on 27 Jun 2020
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');"

  0 Comments

Sign in to comment.

Answers (1)

Sindar
Sindar on 27 Jun 2020
Edited: Sindar on 27 Jun 2020
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 Comment

Sindar
Sindar on 27 Jun 2020
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

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!