MATLAB Answers

Is there a way to change the range function of xlsread based on a variable input?

1 view (last 30 days)
Reuben Stewart
Reuben Stewart on 30 May 2020
Edited: dpb on 30 May 2020
What I want to do is to take values from a large excel file and using matlab create a text file.
Right now I have my code reading individual cell values and assigning them to a variable.
The variable is then input into a string of text using fprintf, as below,
fprintf(op, ' FIle Name : %s\n', sname);
Is there a way to take an input from a user that changes the range in the xlsread functions.
i.e. for the code below,
var = input('enter excel column: ', 's');
variable1 = xlsread('filename.xlsx', 'var4:var4');
variable2 = xlsread('filename.xlsx', 'var115:var115');
so that if, for example, a user input D
the function would read it as,
variable1 = xlsread('filename.xlsx', 'D4:D4');
variable2 = xlsread('filename.xlsx', 'D115:D115');

  0 Comments

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 30 May 2020
var = input('enter excel column: ', 's');
variable1 = xlsread('filename.xlsx', sprintf('%s4:%s4', var, var));
variable2 = xlsread('filename.xlsx', sprintf('%s115:%s115', var, var));

  0 Comments

Sign in to comment.

More Answers (1)

dpb
dpb on 30 May 2020
Edited: dpb on 30 May 2020
Sure (altho it's probably as fast and less hassle to just read read the whole spreadsheet and then just select the results wanted). If you really are using xlsread and calling it more than once on the same file, it's almost certainly slower because xlsread will open/close the file and the COM interface every call.
cols=input('Enter two (2) columns separated by whitespace or delimiter: ','s');
cols=string(split(cols));
rows=[4;115];
rnges=compose('%s%d:%s%d',cols,rows,cols,rows);
returns
>> rnges
rnges =
2×1 cell array
{'A4:A4' }
{'D115:D115'}
>>

  0 Comments

Sign in to comment.

Products


Release

R2020a

Translated by