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.

답변 (1개)

Maitreyee Mordekar
Maitreyee Mordekar 2017년 1월 23일
편집: Maitreyee Mordekar 2017년 1월 23일

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

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);
kcin
kcin 2017년 1월 23일
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.
kcin
kcin 2017년 1월 23일
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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

질문:

2017년 1월 20일

댓글:

2017년 1월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by