Using a loop to create multiple variables

5 ビュー (過去 30 日間)
Neil
Neil 2014 年 6 月 10 日
編集済み: dpb 2014 年 6 月 10 日
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
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.
Neil
Neil 2014 年 6 月 10 日
A cycle here could be 141-231 rows of data within Array "A". Since A has the numbers 1 to 500 before each cycle I need to use array "c".
I want to create multiple arrays (X and Y values) so I can analyse each cycle later on in the code. But I only need every 25th, 50th or 100th cycle.

サインインしてコメントする。

回答 (3 件)

James Tursa
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;
  1 件のコメント
Neil
Neil 2014 年 6 月 10 日
While that is close, it wouldn't quite work. Array 'A' has has many data points (varying in size) representing each cycle. If I put that in, I wouldn't be skipping cycles but data points (and not the correct amount).
thank you though

サインインしてコメントする。


dpb
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...

Andrew
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 ExchangeMatrix Indexing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by