Using a loop to create multiple variables
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
dpb
2014-6-10
>> 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.
回答(3 个)
James Tursa
2014-6-10
编辑:James Tursa
2014-6-10
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
2014-6-10
编辑:dpb
2014-6-10
...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 个评论
Andrew
2014-6-10
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!