How to use strings in function

1 vue (au cours des 30 derniers jours)
Osama Anwar
Osama Anwar le 1 Jan 2021
Commenté : Osama Anwar le 1 Jan 2021
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 commentaires
Osama Anwar
Osama Anwar le 1 Jan 2021
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 le 1 Jan 2021
Morover the value in ExcelColumn is stored as 1x4char

Connectez-vous pour commenter.

Réponse acceptée

Rik
Rik le 1 Jan 2021
Modifié(e) : Rik le 1 Jan 2021
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 commentaire
Osama Anwar
Osama Anwar le 1 Jan 2021
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Downloads dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by