Cells detect and Cells numers in array

Hello,
I've got an array out of a scope simulation in simulink. In this array i've got the time in 1st column and the value of the signal in the 2nd. My max step size is 10sec. so the time data is from 0 to 1000 per 10 ( there is more values in time column but thats not the problem). I want to know what is the value of B1(the cell location as it is in excel) if A1=0. e.g. what is the value of the signal when time is 0 ? Then what is the value when the time is 60 sec and so on.
Thanks in advance !

 Accepted Answer

M1 = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/701747/1.xlsx')
M1 = 65926×2
0 232.6044 0.0000 232.6044 0.0000 232.6044 0.0000 232.6044 0.0000 232.6044 0.0000 232.6044 0.0001 232.6044 0.0003 232.6044 0.0015 232.6044 0.0074 232.6044
time_desired = [0, 10, 60, 90, 120, 180]; % Define As Desired
interpolated_signal = interp1(M1(:,1), M1(:,2), time_desired.', 'linear')
interpolated_signal = 6×1
232.6044 232.6119 232.6492 232.6716 232.6940 232.7388
figure
plot(M1(:,1), M1(:,2),'-b')
hold on
plot(time_desired, interpolated_signal, 'rs')
hold off
grid
xlabel('time')
ylabel('signal')
xlim([0 200]) % Restrict Axis View To Sho9w Detail
Make necessary changes to get the result you want.
.

7 Comments

Thank you so much ! That was what i need. :)
i've one more difficult question.
Well i get what you make here with interp1, but how can i get the average of the data between the range ot the time.
What i mean is it the "time_desired = [0, 10, 60, 90, 120, 180];"
a What is the average value of the signal between 0 and 10 sec e.g. if there is 15 values between 0 and 10 sec. what is their average and then what is the average of the values between 10 and 60 sec and so on.
I will be so grateful if u can help with that one too!
Thank you alot!
As always, my pleasure!
The mean values claculation is a bit more complicated, however definitely possiible:
format shortG
M1 = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/701747/1.xlsx')
M1 = 65926×2
0 232.6 1.5214e-08 232.6 9.1282e-08 232.6 4.7162e-07 232.6 2.3733e-06 232.6 1.1882e-05 232.6 5.9425e-05 232.6 0.00029714 232.6 0.0014857 232.6 0.0074286 232.6
format short
time_desired = [0, 10, 42, 60, 90, 115, 121.5, 180]; % Define As Desired
interpolated_signal = interp1(M1(:,1), M1(:,2), time_desired.', 'linear')
interpolated_signal = 8×1
232.6044 232.6119 232.6358 232.6492 232.6716 232.6903 232.6951 232.7388
meanvals = zeros(numel(time_desired)-1,3);
N = 150; % Interpolation Length
for k = 1:numel(time_desired)-1
xv = linspace(time_desired(k), time_desired(k+1), N);
yv = interp1(M1(:,1), M1(:,2), xv);
meanvals(k,:) = [time_desired(k), time_desired(k+1), mean(yv)];
end
MeanIncrementValues = array2table(meanvals, 'VariableNames',{'Time_1','Time_2','Interval_Mean'})
MeanIncrementValues = 7×3 table
Time_1 Time_2 Interval_Mean ______ ______ _____________ 0 10 232.61 10 42 232.62 42 60 232.64 60 90 232.66 90 115 232.68 115 121.5 232.69 121.5 180 232.72
figure
plot(M1(:,1), M1(:,2),'.-b')
hold on
plot(time_desired, interpolated_signal, 'rs')
hold off
grid
xlabel('time')
ylabel('signal')
xlim([0 200]) % Restrict Axis View To Show Detail
The interpolated times may not always correspond to times in the original data matrix (I added a few to emphasize that), so rather than computing the mean based on the increment between them, this creates a common interpolation length (‘N’), and then computes the average of the interpolated values. This approach also lends itself to calculating the mean slope, or linear, polynomial, or nonlinear regressions between the points.
.
Hello again,
I'm so happy that u help me out! I will use this for my work for sure.
Just to tell you what disturbing me.
1st when i write this 2 rows :
test_results = average(M1(1:74,2)) - row 1
testi_results2 = average(M1(75:134,2)) - row 2
the 1st row give a different answer from the table of MeanIncrementValues
so i find out that's because somehow ( sorry im not good at coding) u ignores the value that are divided by 10 with a remainder other than 0. That is perfect for me !
the 2nd rows shows me again different answer
here i found out that the calculation of the 2nd average is takeing the 600 sec value e.g. when u make the 1st average u actually dont need to use the 0 sec value cuz it is already used in the last average calc.
so here is the same, when the average from 10 to 600 is made, it needs to make next average from 610 to 1200 because 600 is already taken in the last average calculation.
when i do this changes;
testi_results = average(M1(74:134,2)) - row 2
it gives me almost the same number ( only after 6 characters after the decimal point the numbers change) witch is totaly fine for me!
Thanks in advance!
If those little things are not change, its still fine because the relative error is small !
As always, my pleasure!
The end points are included in the mean value calculations by definition. You can use MATLAB indexing conventions to eliminate the first or last points in the calculations, if you want to.
That would be for example:
meanvals(k,:) = [time_desired(k), time_desired(k+1), mean(yv(1:end-1))];
to not include the last point in the calculation, or something similar to eliminate the first point ‘yv(2:end)’, or both ‘yv(2:end-1)’. Choose whatever works best for you.
.
That was what i looking for !
Thank you so much! I can't describe how greatful I am.
Wish u best of luck and stay save!
Great!
As always, my pleasure!
You, too!
.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!