Optimize variable creation from xlsread

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)

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

Whatever you do, do not start numbering variables. Use indexing as Maitreyee has done.
Personally, I would not even create the variables z, c, w, etc. and instead just reshape the input into a 3D matrix:
numvariables = 5;
M = reshape(M, size(M, 1), [], numvariables);
Your z variable is then M(:, :, 1), c is M(:, :, 2), etc. So for example the modulus is:
v = M(:, :, 2) .* M(:, :, 4) ./ M(:, :, 5);
Thankyou for the comment, I like your solution. How would I call on each vector with the 3d matrix? following this I am conducting paired ttest's within each 'variable' (all columns compared against each other within modulus, area....etc)
I'm not sure I understand the compared against each other. You can t test all the columns of all the variables at once simply with:
h = ttest(M); %after it's been reshaped.
h will be a 1 x ncols x nvariables logical matrix.
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.

Categories

Asked:

on 20 Jan 2017

Commented:

on 23 Jan 2017

Community Treasure Hunt

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

Start Hunting!