How can I loop each line of a script as the conditional in a new if statement?

2 views (last 30 days)
I have a large number of conditional statements (4280) that are in a script. From each conditional statement I need to analyze and manipulate it into a specific matrix and then have it put the results from each conditional state into one final matrix. The analysis and steps are the same for each conditional statement so a loop should do the trick, i just cant figure out how to put the initial script into each loop. I believe something like fgetl should work, but cant figure it out. Any help is appreciated. THANKS!
%Simplified Example
%Conditionals from the script
x(i,1)>0.5 & x(i,2)<0.5
x(i,1)>0.5 & x(i,2)>0.5
%...And so on with more complexity
for i=1:10
if (lines from the script)
xmean=grpstats(x2,3) %for the conditional in line 1
%then repeat this over all the lines into a matrix of all the values and conditionals.
%then have a final matrix of something like ('conditional number','true of each conditional','value')
Bus141 on 29 Apr 2015
%Here would be a similar model
%Creating an example matrix
%in the actual matrix the values are of no pattern, the above is done for demonstration
%I am trying to get the values from column 3 through 5 given they meet a set of conditional criteria from columns 1 and 2.
%Below would be a similar script line
x.1>0 & x.2>0 %criteria i=1
x.1<1 & x.2>0 %criteria i=2
x.1<1 & x.2<1 %criteria i=3
%And so on, with similar code though lines equal just over 4000 options
%From this I need to analyze the data that have met each of the conditional criterias above
%Analyzing the data that meet criteria i.
%In algrebraic notation this would be an index for each criteria from the script
f=z(:,3); %this would be f subscript i
p=z(:,4:5); %p subscript i
%final matrices
F(i,:,:)=f %with an entry for each analyzed f into the final F
P(i,:,:)=p %with an entry for each analyzed p into the final P matrix
%Ideally looping the previous 12 lines for all conditional criteria
I am interested in looping the analzying and formatting for each criteria that is met all into 2 final 3D matrices, with 1 dimension of each matrix for each critieria condition. I have all of the conditional criteria written (though long and ugly), any help in preventing me from having to re-write the code for each line of conditional criteria code.

Sign in to comment.

Answers (1)

Joseph Cheng
Joseph Cheng on 28 Apr 2015
Edited: Joseph Cheng on 28 Apr 2015
Well.... I too am not entirely sure what you're trying to accomplish but if the formatting of your conditional script is always in the format "x(i,1){operator}{number} & x(i,2){operator}{number}" then we can use sscanf to parse out the conditions for if statements or to a switch/case. the pattern can be adapted to cover more complex conditions but without an example i'll only use it in the example below:
string = ['x(i,1)>0.5 & x(i,2)<0.5';'x(i,1)>0.5 & x(i,2)>0.5']
A = sscanf(string(ind,:),['x(i,1)%1s%f & x(i,2)%1s%f'])
which would return
A =
where 62 and 60 are the decimal representation of < and >. regexp() would possibly be better to capture more complicated cases but haven't thought about it toomuch since there isn't an example
This would get you away from using the frowned upon eval


Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!