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

1 view (last 30 days)
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!

Accepted Answer

Geoff Hayes
Geoff Hayes on 5 Aug 2014
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 Comments
Lindsay
Lindsay on 5 Aug 2014
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 on 5 Aug 2014
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).

Sign in to comment.

More Answers (0)

Categories

Find more on Simulink in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!