Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help regarding Use of basic loop in matlab

Status
Not open for further replies.

abaqusian

Materials
Sep 7, 2012
26
Hi everybody,

At present, I am learning to work with matlab. I want to learn how to implement a basic loop for the following steps I perform.
I have an excel file named "book" in format (.xlsx). I have two columns with ten rows each as shown below :

Code:
serial  value  
1        0.15
2        0.20
3        0.52
4        0.24
5        0.05
6       -2.05

What I want to do ?

(i) Read the excel file in xlsx into matlab
(ii) For first 3 values in Column_1 , multiply with value 2 and Assign to a new array. Lets say array as "a"
(iii)For last 3 values in column_1 , multiply with value 3 and Assign to a new array. Lets say array as "b"
(iv) Concatenate the new arrays "a" and "b" into a single array . Lets say array as "c"
(v) Assign a new array for column_2 of the excel file, assign an array "d"
(vi) plot array "c" vs "d"

What I did ?

Code:
data=xlsread('book');
a=data(1:3,1)*2;
b=data(4:6,1)*3;
c=[a;b];
d=data(:,2);
plot(c,d)

What I wish to do ?

How to implement a loop to automate this process instead of writing separate syntax as I have did above . How to get to the same result with a use of loop in matlab ?

Kindly cast your opinion to optimise the process.

Thanks from Basic Matlab Learner

 
Replies continue below

Recommended for you

Additional information :

Since, I work with large number of values (row wise) in Excel sheet in a single column , I want to multiply every 10 values in column1 with a factor , then next 10 values in column1 with an another factor And SO ON . I want to do this in a loop.
 
While a loop would certainly help, I read somewhere that if you're using a loop in Matlab you're probably doing it wrong. I thus offer this non-loop suggestion:

Code:
factors = [2 3]; % specify your multiplicative factors
every = 10; % specify the number of rows to apply each factor to

factors = repmat(factors, 1, every)'(:); % repeat, transpose and serialise the factors to form a column vector of factors

c = data(:,1).*factors
d = data(:,2)
plot(c,d)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor