Borrar filtros
Borrar filtros

How to find length of a field of an object?

7 visualizaciones (últimos 30 días)
C A el 8 de Mzo. de 2022
Respondida: Sudarsanan A K el 20 de Dic. de 2023
I have an object that contains signal values. I want to find the length of the signal inbetween two elements in the field called uppFWHMSignal and lowFWHMSignal.
function FWHM(Obj,signal)
for CellNr=StartCellNr:EndCellNr
for iFrame=1:length(Obj.CompCellList{CellNr})%ib:id
Obj.Data{CellNr}{iFrame}.([signal,'uppFWHMSignal'])= max(max(Obj.Datat{CellNr}{iFrame}.(signal))/2);
Obj.Data{CellNr}{iFrame}.([signal,'lowFWHMSignal'])= min(max(Obj.Data{CellNr}{iFrame}.(signal))/2);
Obj.Data{CellNr}{iFrame}.([signal,'length'])=length(Obj.Data{CellNr}{iFrame}.(signal)); %length of the signal
Thank you

Respuestas (1)

Sudarsanan A K
Sudarsanan A K el 20 de Dic. de 2023
It seems like you're trying to define a function in MATLAB that calculates the Full Width at Half Maximum (FWHM) for a given signal within an object. The FWHM is a measure of the width of a signal at half of its maximum amplitude. However, there are a few issues with the code you've provided, and it does not actually calculate the FWHM.
Here is a corrected version of the function that attempts to find the indices where the signal crosses the half-maximum threshold and then calculates the width between these two points with a sample MATLAB script to validate the functionality:
%% Sample MATLAB script to validate the FWHM function
% Create a synthetic signal with a clear peak
signalData = [1, 2, 3, 4, 5, 4, 3, 2, 1];
% Construct the object with the expected structure
Obj = struct();
Obj.Data = {{struct('mySignal', signalData)}};
% Define the signal name
signalName = 'mySignal';
% Call the FWHM function with the sample object and signal name
Obj = FWHM(Obj, signalName);
% Accessing the FWHM field with the correct name
fwhmField = [signalName '_FWHM'];
% Display the results
disp(['FWHM: ', num2str(Obj.Data{1}{1}.(fwhmField))]);
% Validate the results
expectedFWHM = 5; % The expected FWHM for the sample signal
actualFWHM = Obj.Data{1}{1}.(fwhmField);
if actualFWHM == expectedFWHM
disp('The FWHM function is working correctly.');
disp('The FWHM function is not working correctly.');
The FWHM function is working correctly.
%% FWHM function definition
function Obj = FWHM(Obj, signalName)
% Calculate the FWHM for a signal within an object
for iCell = 1:length(Obj.Data)
for iFrame = 1:length(Obj.Data{iCell})
% Extract the signal data
signalData = Obj.Data{iCell}{iFrame}.(signalName);
% Find the maximum value of the signal and its index
[maxValue, ~] = max(signalData);
halfMax = maxValue / 2;
% Find indices where the signal is greater than half the max
indicesAboveHalfMax = find(signalData >= halfMax);
% Find the first and last indices where the signal is above half max
firstIndex = indicesAboveHalfMax(1);
lastIndex = indicesAboveHalfMax(end);
% Calculate the FWHM
fwhmValue = lastIndex - firstIndex + 1;
% Store the results in the object
Obj.Data{iCell}{iFrame}.([signalName '_FWHM']) = fwhmValue;
The provided MATLAB function "FWHM" calculates the Full Width at Half Maximum (FWHM) for a specified signal within each frame of data for each cell in an object. The function operates as follows:
  1. Iterate over cells and frames: The function loops through each cell ("iCell") in the "Obj.Data" array and then through each frame ("iFrame") within each cell's data.
  2. Extract signal data: For the current frame of the current cell, the function retrieves the data array corresponding to the signal specified by "signalName".
  3. Find maximum value: The function identifies the maximum value of the signal data array. It uses this value to calculate the half-maximum level ("halfMax"), which is half of the signal's peak value.
  4. Identify indices above half-maximum: The function then finds all indices of the signal data where the value is greater than or equal to the half-maximum level. These indices represent the points on the signal that are at or above half the peak amplitude.
  5. Determine FWHM bounds: From the indices identified in the previous step, the function determines the first and last indices ("firstIndex" and "lastIndex") where the signal is equal to or exceeds the half-maximum level. These indices mark the boundaries of the FWHM.
  6. Calculate FWHM: The FWHM value is calculated as the difference between the last and first indices plus one. This represents the width of the signal at the half-maximum level.
  7. Store FWHM in object: Finally, the calculated FWHM value is stored back into the object under the current cell and frame, with the key being the original "signalName" appended with "_FWHM".
The function modifies the original object by adding the FWHM information for each signal in each frame of each cell, allowing for further analysis or reporting of the signal's characteristics.
Further, for better understanding of the Object Oriented Programming and operations with objects in MATLAB, you can refer the following MathWorks documentations:
I hope this helps!





Community Treasure Hunt

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

Start Hunting!

Translated by