How can I store y-values in one place after drawing data from different Excel sheets?

조회 수: 1 (최근 30일)
I am somewhat new to Matlab and often know what I need to do but am unable to execute it. Anyway, I am pulling amplitude data from various Excel sheets and plotting it on 1 figure (so, group data). The issue is that I need to take this data I've congregated and generate a best fit line for the whole group. I imagine I need all x and y values from every subject in order to do so. However, I can't figure out how to build a new vector with all of the imported data. See code below:
for isubj = 1:length(SUBJECTS)
groupamp = zeros(1, 128);
if isubj == 1
qthres = xlsread(xlsfile, 1,'J3:J16');
peakamp = xlsread(xlsfile, 1, 'L3:L16');
scalae = xlsread(xlsfile, 1, 'C3:C16'); % can only use elecs 2-15 here because of qthres!
[STi, STj, ST] = (find(scalae == 1 & peakamp > 0));
[SMi, SMj,SM] = (find(scalae == 2 & peakamp > 0));
[SVi, SVj,SV] = (find(scalae == 3 & peakamp > 0));
[peakampi, peakampj] = find(peakamp > 0);
elseif isubj == 2
qthres = xlsread(xlsfile, 2,'J3:J16');
peakamp = xlsread(xlsfile, 2, 'L3:L16');
scalae = xlsread(xlsfile, 2, 'C3:C16');
[STi, STj, ST] = (find(scalae == 1 & peakamp > 0));
[SMi, SMj,SM] = (find(scalae == 2 & peakamp > 0));
[SVi, SVj,SV] = (find(scalae == 3 & peakamp > 0));
[peakampi, peakampj] = find(peakamp > 0);
..this goes to isubj = 8 and then...
plot(qthres(STi),peakamp(STi), 'ko', 'LineWidth', 1.5);
hold on;
plot(qthres(SMi),peakamp(SMi), 'gd', 'LineWidth', 1.5);
hold on;
plot(qthres(SVi),peakamp(SVi), 'b^', 'LineWidth', 1.5);
hold on;
groupamp(1, peakampi) = [peakamp(peakampi)'];
end
xx = 30:5:60; fit = polyfit(qthres(peakampi),peakamp(peakampi),1); plot(xx, fit(1)*xx+fit(2),'k-', 'LineWidth', 2);
Obviously the polyfit is incorrect here. I just hard-coded the preallocation with the maximum possible value (in this case, 8 subjects by 16 possible amplitudes).
Thanks for helping a beginner!
  댓글 수: 1
Lindsay
Lindsay 2014년 8월 4일
Oh, and obviously the groupamp stuff is wrong too. This is what I'm trying to work out. Thanks!

댓글을 달려면 로그인하십시오.

채택된 답변

Geoff Hayes
Geoff Hayes 2014년 8월 5일
Lindsay - if it just the peakampi data that you wish to concatenate into a single vector, then you could do something like the following (note that I simplified it based on the assumption that the only difference between subjects is the worksheet of the Excel document
% declare an empty vector for all peakampi data
allPeakampi = [];
for isubj = 1:length(SUBJECTS)
qthres = xlsread(xlsfile, isubj,'J3:J16');
peakamp = xlsread(xlsfile, isubj, 'L3:L16');
scalae = xlsread(xlsfile, isubj, 'C3:C16'); % can only use elecs 2-15 here because of qthres!
[STi, STj, ST] = (find(scalae == 1 & peakamp > 0));
[SMi, SMj,SM] = (find(scalae == 2 & peakamp > 0));
[SVi, SVj,SV] = (find(scalae == 3 & peakamp > 0));
[peakampi, peakampj] = find(peakamp > 0);
% save the peakampi data - ensure that it is a row vector
if ~isrow(peakampi)
peakampi = peakampi';
end
allPeakampi = [allPeakampi peakampi];
end
Outside of the for loop, you will have the all peak amplitude data (i only) in the vector allPeakampi. I've done away with the pre-allocation of memory for simplicity and because we don't expect all that many elements (at most 120 or so).
If you need all data from other vectors, then you can follow this pattern. Try the above and see what happens!
  댓글 수: 2
Lindsay
Lindsay 2014년 8월 5일
Geoff, thank you so much! I will try this and let you know how it works. One other question though: I end up plotting peakamp(peakampi) so I get the actual values and not just the indices. Could I do the same thing you did above but just using the peakamp(peakampi) syntax? I can't tell you how much I appreciate the help!
Geoff Hayes
Geoff Hayes 2014년 8월 5일
No problem, Lindsay. Yes, if you just want the peakamp data (and not the indices) then just do the same as above using the syntax that you have described (so long as peakamp(peakampi) is a row vector).

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by