Using a loop to create multiple variables
4 views (last 30 days)
Show older comments
Hello everyone,
I am currently trying to create a program that would isolate certain data within a VERY large set of data (101304x3 matrix).
So at the beginning of the code, the user is asked to input whether the cycles we care about are every 25, 50 or 100 cycles (isolated as variable CycleSkip) for 500 cycles of data.
What the following code ATTEMPTS to do, is create an array in X (Strain) and Y(Stress) for either every 25th cycle, 50th cycle or 100th cycle. Basically, as many arrays of X and Y data as needed.
If we ignore the 1st cycle, I thought it might look something like this;
d=0;
while d<500
C(CycleSkip+d)_Strain=A(c(CycleSkip+d,1):c(CycleSkip+d+1,1)-2,2)/L0;
C(CycleSkip+d)_Stress=A(c(CycleSkip+d,1):c(CycleSkip+d+1,1)-2,3)/Area;
d=d+CycleSkip;
end
Let me just say I'M AWARE "C(CycleSkip+d)_Strain" and "C(CycleSkip+d)_Stress" won't actually work for the purposes I intend them too. That's why I'm asking for help in this matter.
The variable "d" would be the counter, the variable "c" is a vector which has the row numbers of the start of each cycle in array "A"
Is this even possible, or is there a better way to do it?
Thanks for your time and help!
Neil
2 Comments
dpb
on 10 Jun 2014
>> 101304*3*8/1024/1024
ans =
2.3187
>>
So the array, while large, isn't all that huge (2+ MB) by today's standards.
Clarify precisely which data you want, please. What is a "cycle" here and do you mean to basically decimate the whole file by that ratio or to select a sequential group of length L a distance D apart or what?
And, why multiple arrays; why not just work on the data selected; it isn't all that big at all once you've reduced it so drastically.
Answers (3)
James Tursa
on 10 Jun 2014
Edited: James Tursa
on 10 Jun 2014
Are you simply trying to do something like this:
D = A(FirstIndex:CycleSkip:FirstIndex+500,:);
Then you can pick off X and Y (2nd and 3rd columns) as:
X = D(:,2) / LO;
Y = D(:,3) / Area;
dpb
on 10 Jun 2014
Edited: dpb
on 10 Jun 2014
...to create multiple arrays (X and Y values) so I can analyse each cycle later on
Then I'd suggest to rearrange to do the analysis of a cycle each time you identify it and then move on to the next.
fmt=['%*f repmat('%f',1,2)]; % 3 floats/rec; skip first
fid=fopen('thedatafile','r'); % open the file
hlines=500; % initial headerlines to skip
while ~feof(fid) % go until run out of data
c=cell2mat(textscan(fid,fmt,nLinesInCycle, ...
'headerlines',hlines,'collectoutput',true));
X=c(:,1)/LO; % make the two variables but might just as well use
Y=c(:,2)/Area; % the array columns instead.
clear c % free up the array not being used...
% do analysis on this cycle here...
...
hlines=nCyclesToSkip*nLinesInCycle+500; % the next number to skip
end
fid=fclose(fid);
I'm still not precisely clear on the structure; just took a guess at where the 500 lines come in and what's actually between the sets with hopefully descriptively-enough named variables you can fix the algebra in computing the size to skip in the hlines value...
0 Comments
Andrew
on 10 Jun 2014
One thing you could do is use genvarname with a struct.
for example
d=0;
while d<500
C.(genvarname([num2str(CycleSkip+d) '_Strain'))=A(c(CycleSkip+d,1):c(CycleSkip+d+1,1)-2,2)/L0;
C(genvarname([num2str(CycleSkip+d) '_Stress'))=A(c(CycleSkip+d,1):c(CycleSkip+d+1,1)-2,3)/Area;
d=d+CycleSkip;
end
which would create a struct with field names of (CycleSip+d)_Stress and (CycleSkip+d)_Strain
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!