How to use strings in function

1 次查看(过去 30 天)
I'm inputing excel data into matlab using formula "xlsread". The variable in which I'm inputing excel data i.e "ETABSresponse" requires to have a certain size depending on size of the variable so that it will not give error "Vectors must be the same length" .
Formula for inputing data the function is of the form
ETABSresponse=xlsread('ETABS.xlsx','A1:ALM7');
But I want it to be of form so that the above problem do not occur.
ETABSresponse=xlsread('ETABS.xlsx','A1:ExcelColumn');
Where ExcelColumn is a function that converts numbers into Excel column index
it gives 'A' for 1, 'Z' for 26 and 'AA' for 27 and so on
ExcelColumn=[ExcelColumn(L/0.001+1) num2str(7)]
So if L=1 ExcelColumn(L/0.001+1) will be ALM and ExcelColumn will be 'ALM7'
I want to use second part of range as variable that can be adjusted according to my needs but the formula
ETABSresponse=xlsread('ETABS.xlsx','A1:ExcelColumn');
won't work. How can I make it work?
  2 个评论
Osama Anwar
Osama Anwar 2021-1-1
ALM7 is saved as 'ALM7' in the variable ExcelColumn in Workspace section of Matlab. Maybe the problem is with single quotes around ALM7? If it is then how can I resolve that?
Osama Anwar
Osama Anwar 2021-1-1
Morover the value in ExcelColumn is stored as 1x4char

请先登录,再进行评论。

采纳的回答

Rik
Rik 2021-1-1
编辑:Rik 2021-1-1
You need to concatenate the output of the function you created. That way you get a single char vector as the range specification.
ETABSresponse=xlsread('ETABS.xlsx',['A1:' ExcelColumn]);
I sometimes use a function like the one below to create the column name:
L=1;
ExcelColumn=sprintf('%s%d',get_ExcelColName(L/0.001+1),7);
disp(ExcelColumn)
ALM7
function ExcelColName=get_ExcelColName(col_val)
%convert a col value to the Excel col name (so 27 to AA, 705 to AAC)
validateattributes(col_val,{'numeric'},{'scalar','nonzero','integer'})
base=26;
col_val=col_val-1;
digs=floor(log(col_val)/log(base))+1;%number of 'digits' (i.e. letter positions)
ExcelColName=zeros(1,digs);
for cur_d=digs:-1:1
ExcelColName(cur_d)=mod(col_val,base);
col_val=(col_val-ExcelColName(cur_d))/base;
end
if isempty(ExcelColName),ExcelColName=0;end%in case of col_val input is 1
ExcelColName(end)=ExcelColName(end)+1;
ExcelColName=char(ExcelColName+64);
end
  1 个评论
Osama Anwar
Osama Anwar 2021-1-1
First one worked for me fine I downloaded the ExcelColumn function from File Exchange myself as I am not a coder. Thank you so much for helping me out.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Downloads 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by