I have a loop but only the last value saves
Show older comments
i want to see the individual peak shifts for 520. i see it as a weird output with disp but i want it in my variables not an output in the command window. the bolded line is what i want to see all the values for before the mean. for sens_peak_shift_data_520 i only see the means there but i do not want to alter that because i am graphing from that variable. please help!
for i= 1:length(sens_legend)
data_set= sens_legend{i};
peak_520=[]; prom=[]; width=[]; peak_260=[];
sens_shifted_peak=[];
for k = 1:length(data_set)
data= data_set{k};
absorbance= data(:, 2)/(max(data(321:1221, 2)));
wavelength= data(:, 1);
data_n = [wavelength, absorbance];
[pk, loc, w, p]= findpeaks(absorbance, wavelength, "MinPeakProminence", 0.08);
[maxvalue, index] = max(data_n(651:1221,2));
peak_520 = [abs(data_n(index+650,1))-520, peak_520];
disp(peak_520)
sens_shifted_peak= [sens_shifted_peak, peak_520];
end
sens_peak_shift_data_520= [sens_peak_shift_data_520; [mean(peak_520, 2) std(peak_520,0,2) CI(1) CI(2) CI(3) CI(4)]];
8 Comments
Adam Danz
on 23 Mar 2023
I formatted your code to make it easier to read. This also un-formatted the bold line.
It sound like you want the values stored in sens_shifted_peak.
Chelsie Boodoo
on 23 Mar 2023
Adam Danz
on 23 Mar 2023
It looks like you're storing those values in sens_shifted_peak assuming you aren't getting an error message.
Chelsie Boodoo
on 23 Mar 2023
Chelsie Boodoo
on 23 Mar 2023
Adam Danz
on 23 Mar 2023
It's difficult to visualize what's going on here. You're increasing the number of values in this line
peak_520 = [abs(data_n(index+650,1))-520, peak_520];
and then storing them again in this line
sens_shifted_peak= [sens_shifted_peak, peak_520];
We don't know what sens_legend or data_set are, particularly their size, so we have no way of confirming what size the output should be. If you provide variable values so we can run your code, that would help.
Chelsie Boodoo
on 23 Mar 2023
Edited: Walter Roberson
on 24 Mar 2023
Chelsie Boodoo
on 24 Mar 2023
Answers (1)
Dinesh
on 5 Apr 2023
Hi Chelsie.
It seems that you want to store the individual peak shifts for 520 in a variable rather than displaying them in the command window using "disp". To achieve this, you can create an additional variable to store the peak shifts for each dataset in "sens_legend". I added a new variable named "all_peak_shifts_520" that will store the peak shifts for all datasets.
You can create the "all_peak_shifts_520" variable as an empty cell array before the main for loop:
all_peak_shifts_520 = {};
After the inner for loop, you can store the peak shifts for the current dataset.
all_peak_shifts_520{end+1} = peak_520;
Now, you can access the individual peak shifts for 520 for each dataset in the "sens_legend" by indexing "all_peak_shifts_520". For example, to access the peak shifts for the first dataset, use "all_peak_shifts_520{1}". This will not affect the "sens_peak_shift_data_520" variable.
3 Comments
Chelsie Boodoo
on 7 Apr 2023
Dinesh
on 7 Apr 2023
Hi Chelsie. I'm not able to understand why this is happening. But, the code will store all the peak shifts for every item in "sens_legend". Since the inner loop calculates all the peak shifts for each item in "sens_legend", the following line after the inner for loop will store that peak shift in a new array.
all_peak_shifts_520{end+1} = peak_520;
Either the place where you included this piece of code is wrong or the sizes of "sens_legend" and "data_set" might just be 1 in this case since its showing 1x1 cell in the workspace.
Chelsie Boodoo
on 7 Apr 2023
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!