store data in an array

14 views (last 30 days)
Along with saying hello, I need help storing information. I did a for loop so that it was storing data, but when dt = [] is not stored, also if you could help me that when it happens that dt = [], the threshold value is 1.5, it would be 0 and thus not have the error that dt was an empty set, in short, that when dt = [], the threshold is 0 so that I can calculate the loop and also store it in the dt matrix.
Thanks greetings.
I attach .txt with the data, a function and the code I leave it copied in the questions panel.
close all, clear all, clc
amplitude=load('amplitude.txt');
x=0:10:4*3600;
threshold = 1.5;
for j=1:7
y=amplitude(:,j);
[val,ind] = max(y);
x_peak = x(ind);
[t0_pos,s0_pos,t0_neg,s0_neg]= crossing_V7(y,x,threshold,'linear');
[minValue, closestIndex1] = min(abs(t0_pos - x_peak));
closestValue_pos = t0_pos(closestIndex1);
[minValue, closestIndex2] = min(abs(t0_neg - x_peak));
closestValue_neg = t0_neg(closestIndex2);
dt = abs(closestValue_neg - closestValue_pos)./60
end

Accepted Answer

Walter Roberson
Walter Roberson on 3 Jun 2021
amplitude=load('amplitude.txt');
x=0:10:4*3600;
threshold = 1.5;
cols = size(amplitude,2);
all_dt = zeros(1,cols);
for j=1:cols
y=amplitude(:,j);
[val,ind] = max(y);
x_peak = x(ind);
[t0_pos,s0_pos,t0_neg,s0_neg]= crossing_V7(y,x,threshold,'linear');
[minValue, closestIndex1] = min(abs(t0_pos - x_peak));
closestValue_pos = t0_pos(closestIndex1);
[minValue, closestIndex2] = min(abs(t0_neg - x_peak));
closestValue_neg = t0_neg(closestIndex2);
dt = abs(closestValue_neg - closestValue_pos)./60;
if isempty(dt); dt = 0; end
all_dt(j) = dt;
end
  4 Comments
ignacio bobadilla tapia
ignacio bobadilla tapia on 3 Jun 2021
It executes the code, but I get an empty set in the results of the code.
Walter Roberson
Walter Roberson on 4 Jun 2021
The only way that code can return empty is if the loaded amplitude is empty. In such a case,, cols would come out as 0 and all_dt would be a 1 x 0 matrix.
In all other cases, the result in all_dt could potentially turn out all 0, but cannot turn out empty.

Sign in to comment.

More Answers (0)

Categories

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