MATLAB Answers

Optimize variable creation from xlsread

2 views (last 30 days)
kcin
kcin on 20 Jan 2017
Commented: kcin on 23 Jan 2017
Hi, how can I optimize the below script so I can create as many of the 5 variables (z,c,w,y,v) as there is available columns to pull from? Currently using below as a discrete method.
M=xlsread(filespec);
%Failure Load
z1 = M(:,1);
z2 = M(:,8);
z3 = M(:,15);
z4 = M(:,22);
z5 = M(:,29);
%Stiffness
c1 = M(:,2);
c2 = M(:,9);
c3 = M(:,16);
c4 = M(:,23);
c5 = M(:,30);
%Deformation
w1 = M(:,3);
w2 = M(:,10);
w3 = M(:,17);
w4 = M(:,24);
w5 = M(:,31);
%Length
x1 = M(:,4);
x2 = M(:,11);
x3 = M(:,18);
x4 = M(:,25);
x5 = M(:,32);
%Area
y1 = M(:,5);
y2 = M(:,12);
y3 = M(:,19);
y4 = M(:,26);
y5 = M(:,33);
%Modulus
v1=c1.*(x1./y1);
v2=c2.*(x2./y2);
v3=c3.*(x3./y3);
v4=c4.*(x4./y4);
v5=c5.*(x5./y5);
Currently calling on columns based on knowledge of what the excel file contains. How can I automate to call on all columns with data in (each variable always separated by 7 columns). Thank you for any offered help.

Answers (1)

Maitreyee Mordekar
Maitreyee Mordekar on 23 Jan 2017
Edited: Maitreyee Mordekar on 23 Jan 2017
Hi,
I assume that you want to automate the code wherein you will not have to type in the assignment for each of the variables. You can use vectorization in MATLAB to do the same. The reference link for the same is as follows- https://in.mathworks.com/help/matlab/matlab_prog/vectorization.html
The following is one of the ways you can use to obtain the desired result-
i=1;
VarDim=5; % Dimension of the variable
VarSep=7; % Column gap between the variables
index=i:VarSep:VarSep*VarDim;
z=M(:,index); %Failure Load
c=M(:,index+1); %Stiffness
w=M(:,index+2); %Deformation
x=M(:,index+3); %Length
y=M(:,index+4); %Area
v=c.*(x./y); %Modulus
Do note that the elements in the variable 'z1' in your script will correspond to 'z(1)' in this script, 'z2' to 'z(2)' and so on.
  4 Comments
kcin
kcin on 23 Jan 2017
Am i right in assuming this returns statistical difference from mean of each variable? Im looking for column by column assessment. Example for each variable column 1 vs c2,c3,c4,c5. Column 2 vs 3,c4,c5....etc and there associated p-values. Excuse my limited matlab knowledge

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!