How to remove pulsewidth function without shifting my data?

1 visualización (últimos 30 días)
Luis Huerta
Luis Huerta el 8 de Jul. de 2022
Respondida: Karan Singh el 29 de Sept. de 2023
When I try to delete the pulsewidth function, my sensor_data switches from a 3006x3 matrix to a 3000x3 matrix, this causes the code to give me an error "arrays have incompatible sizes for this operation, I want to find a way to plot the data without having to use the pulsewidth function to shift the data so it matches up, is there any other function that could do this ? This is the part of the code
% Import the data
for k=1:size(files)
TestSensor = readtable(files(k,1),opts);
TestInstron = readtable(files(k,2),opts_instron);
% transfer the table data to the array
sensor_data = table2array(TestSensor(2:height(TestSensor),:));
instron_data = table2array(TestInstron(2:height(TestInstron),:));
% use pulsewidth function to get the cross
% need to adjust the 'StateLevels': 0.95, 200 if we don't get the correcct numbers.
[w,initcross,finalcross] = pulsewidth(sensor_data(:,3),sensor_data(:,1),'StateLevels',[0.95*max(sensor_data(:,3)) max(sensor_data(:,3))+201],'Tolerance',45);
gap = finalcross- initcross+Valueshift;
sensor_data_shift = sensor_data(:,1)-initcross;
mask = sensor_data_shift>0;
%Pulling Force
plot(sensor_data_shift.*mask,sensor_data(:,3).*mask,'LineWidth',1.5,'Color',[0.267, 0.447, 0.769])% RGB Blue 68, 114, 196
hold on
%Normal Force
plot(instron_data(:,1)/SampleRate+gap,instron_data(:,3),'LineWidth',1.5,'Color',[0.929, 0.490, 0.192])% RGB Orange 237, 125, 49
%Friction
% RGB Grey 165, 165, 165
legend('Normal Force','Pulling Force','Friction*1000')
grid on
title(NamedGraphTitle)
xlabel('Time (s)')
ylabel('Force (lbs)')
axis([12.5,TimeendX,0,7500])
hold off
Here is picture of the graph

Respuestas (1)

Karan Singh
Karan Singh el 29 de Sept. de 2023
Hi Luis,
From what I understand, the code you provided includes the pulsewidth function, which is used to shift the data so that it aligns correctly for plotting. However, you mentioned that removing the pulsewidth function causes the size of the sensor_data matrix to change, leading to an error when plotting the data. The error message suggests that the size mismatch between the sensor_data and instron_data arrays is causing an issue when trying to plot them together.
Instead of relying on the pulsewidthfunction, you can manually find the shift between the two datasets based on a specific criterion. Assuming if the maximum value in sensor_data corresponds to the start of the alignment. Here’s an example of how you can modify your code to manually align the datasets without using pulsewidth:
% Import the data
for k = 1:size(files)
TestSensor = readtable(files(k,1), opts);
TestInstron = readtable(files(k,2), opts_instron);
% Transfer the table data to the array
sensor_data = table2array(TestSensor(2:height(TestSensor), :));
instron_data = table2array(TestInstron(2:height(TestInstron), :));
% Find the shift or alignment between the datasets
[~, max_index] = max(sensor_data(:, 3)); % Find the index of the maximum value in sensor_data
% Calculate the shift based on the desired criterion
shift = sensor_data(max_index, 1) - instron_data(1, 1);
% Plot the data with the calculated shift
plot(sensor_data(:, 1) - shift, sensor_data(:, 3), 'LineWidth', 1.5, 'Color', [0.267, 0.447, 0.769]) % Pulling Force
hold on
plot(instron_data(:, 1), instron_data(:, 3), 'LineWidth', 1.5, 'Color', [0.929, 0.490, 0.192]) % Normal Force
legend('Normal Force', 'Pulling Force', 'Friction*1000')
grid on
title(NamedGraphTitle)
xlabel('Time (s)')
ylabel('Force (lbs)')
axis([12.5, TimeendX, 0, 7500])
hold off
end
In this modified code, we find the index of the maximum value in the sensor_data array and calculate the shift as the difference between the maximum value's time and the first time in the instron_data array. This shift is then used to align the x-axis values of the two datasets when plotting.
Attached below are some documentation links that you may find helpful:
Hope this helps!
Karan Singh Khati

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by