I would like to write a for loop to calculate fwhm value based on a range of Strain values. Say Strain = 50:10:150 and find the corresponding fwhm values in a vector

2 views (last 30 days)
X = (0:0.01:0.3);
Y = -9200*(X).^2 + 2760*X;
%Plot Graph
% Create figure
figure1 = figure('Color',[1 1 1]);
% Create axes
axes1 = axes('Parent',figure1);
hold(axes1,'on');
% Create scatter
plot(X,Y,'DisplayName','Voltage');
% Create ylabel
ylabel('Strain');
xlabel('Time')
% Find the Given threshold Strain
Strain = 30;
hold on;
yline(Strain, 'Color', 'r', 'LineWidth', 1);
%Find elements where y is above the thresholdHeight
aboveThreshold = Y > Strain;
smallFontSize = 8;
% Label each group.
[groups, numRegions] = bwlabel(aboveThreshold);
fwhm=zeros(1,length(numRegions))';
x1val=zeros(1,length(numRegions))';
x2val=zeros(1,length(numRegions))';
%loop
for r = 1 : numRegions
% Get a mask for this particular group
mask = groups == r;
% Logical "map" of where this group is.
indexes = find(mask);
% Get actual element (index) numbers.
% Find left edge
index1 = min(indexes);
x1 = X(index1);
% Find right edge
index2 = max(indexes);
x2 = X(index2);
x1val(r)=x1;
x2val(r)=x2;
% Compute the full width, half max.
fwhm(r) = x2 - x1;
end

Accepted Answer

Star Strider
Star Strider on 7 Jun 2022
Calculating the width for every value of ‘Strain’ is reasonably straightforward —
X = (0:0.01:0.3);
Y = -9200*(X).^2 + 2760*X;
%Plot Graph
% Create figure
figure1 = figure('Color',[1 1 1]);
% Create axes
axes1 = axes('Parent',figure1);
hold(axes1,'on');
% Create scatter
plot(X,Y,'DisplayName','Voltage');
% Create ylabel
ylabel('Strain');
xlabel('Time')
% Find the Given threshold Strain
Strain = 50:10:150;
hold on;
yline(Strain, 'Color', 'r', 'LineWidth', 1);
Xend = numel(X);
for k1 = 1:numel(Strain)
idx = find(diff(sign(Y-Strain(k1))));
for k2 = 1:numel(idx)
idxrng = max(1,idx(k2)-2) : min(Xend,idx(k2)+2);
xval(k1,k2) = interp1(Y(idxrng), X(idxrng), Strain(k1));
end
wdth(k1) = diff(xval(k1,:));
end
Strain = Strain(:);
Width = wdth(:);
Result = table(Strain, Width)
Result = 11×2 table
Strain Width ______ _______ 50 0.26122 60 0.25263 70 0.24393 80 0.23482 90 0.22537 100 0.21553 110 0.20518 120 0.19428 130 0.18284 140 0.17038 150 0.15728
Calculating the full-width-half-maximum would be similar, however it would be necessary to first calculate the half-maximum value based on every value of ‘Strain’ (half the difference between the value of ‘Strain’ and the fixed maximum) and then do the same calculations.
.
  18 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Contour Plots 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!