Filter löschen
Filter löschen

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

4 Ansichten (letzte 30 Tage)
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');

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 30 Mai 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));

Weitere Antworten (1)

dpb
dpb am 30 Mai 2020
Bearbeitet: dpb am 30 Mai 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'}
>>

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by