How do I combined all peak to become one spectral line?
조회 수: 1 (최근 30일)
이전 댓글 표시
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/195737/image.jpeg)
Hi guys, from the code I have I managed to produce the graph:
%get list of element, prompt user to select one, and get content of element file
filelist = dir('*.txt');
[~, elementlist] = arrayfun(@(f) fileparts(f.name), filelist, 'UniformOutput', false);
[elementindex, ok] = listdlg('ListString', elementlist, 'PromptString', 'Select an element', 'SelectionMode', 'single');
if ~ok, return; end %terminate if user click cancel
elementdata = importdata(filelist(elementindex).name);
assert(isnumeric(elementdata), 'Element does not have data'); %abort if the file does not contain a matrix
%build wavelength array
step = 0.02;
ss = 100;
steparray = -step*ss : step : step*ss;
new_wavelength = (elementdata(:, 1) + steparray); %no loop needed. Requires R2016b or latter
%get row selection
[selectedrow, ok] = listdlg('ListString', compose('%f', elementdata(:, 1)), 'PromptString', 'Select a wavelength', 'SelectionMode', 'multiple');
if ~ok, return; end %terminate if user click cancel
%build Peak vector for selected row
Delta_lambda = 0.2;
int_fact = elementdata(selectedrow, 2);
num = new_wavelength(selectedrow,ss+1)-new_wavelength(selectedrow,:);
denom = Delta_lambda/2;
Peak = (int_fact.*(1./(1+(num/denom).^2)));
%combined peak vector for selected row
plot(new_wavelength(selectedrow, :).', Peak.')
How do I combined all the peak that I chose to become as the graph below bcs I have try by using sum(peaks) but i doesnt work so im not sure how should I do it. One of the expert(Mathworks society) told me to resample each row to the same wavelength before i can sum it but im not sure how should i do it.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/195738/image.png)
I have attached the element file as well. Thanks in advanced.
댓글 수: 3
Guillaume
2018년 9월 14일
For info, (a bit late), this is a continuation of why-am-i-getting-an-error-of-subscripted-assignment-dimension-mismatch
채택된 답변
jonas
2018년 9월 14일
편집: jonas
2018년 9월 14일
They key is to resample the curves along a common x-vector. You can do this by interp1. Just add these new lines of code:
% ... old code
Peak = (int_fact.*(1./(1+(num/denom).^2)));
% ...New code begins here
x=new_wavelength(selectedrow,:);
y=Peak;
% Remove rows with NaNs
x(isnan(y(:,1)),:)=[];
y(isnan(y(:,1)),:)=[];
% Number of peaks
np=size(y,1);
% New xvec
xh=min(x(:)):1e-3:max(x(:)); %new x vector
% Preallocate new peak vector
yh=nan(size(x,1),size(xh,2));
%Interpolate one peak at a time
for i=1:np
yh(i,:)=interp1(x(i,:),y(i,:),xh);
end
% Sum column-wise, exclude nans
ysum=nansum(yh,1);
% Plot
plot(xh,ysum)
First three wavelengths of Ferum:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/195683/image.jpeg)
댓글 수: 5
추가 답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!