how to plot index (rownumbers) in a larger dataset as range?

4 Ansichten (letzte 30 Tage)
S
S am 20 Jan. 2023
Kommentiert: Walter Roberson am 22 Jan. 2023
Hi,
I'm trying to select certain datavalues with a statement from a larger dataset, resulting in a index array. I need this index array for my plot, however I can't get the index array of equal length with the larger dataset. My code is as follows:
A3 = find(data(:,2)>3.3 & data(:,3)>235 & data(:,3)<275); %index array and statement
Unrecognized function or variable 'data'.
figure();
plot(time,data(:,2)); %plot of normal dataset
hold on;
plot(time(A3),data(A3,2),'r.'); %plot that doesn't work (see error code below)
hold off
Resulting in the following error:
Index exceeds the number of array elements (52560).
Error in test_statement (line 88)
plot(time_2018(A3),ws1_2018(A3),'r.');
For your information the data values are as follows:
data 157824x9 double
time 52560x1 double
A3 19967x1 double
  2 Kommentare
Matt J
Matt J am 20 Jan. 2023
Bearbeitet: Matt J am 20 Jan. 2023
Assuming the two variable names that you've shown -- time and time_2018 -- refer to the same variable, they are not the same length as data. So, it is not clear how you would expect the plot would work even if you had run the unindexed version,
plot(time_2018,ws1_2018,'r.');
S
S am 21 Jan. 2023
They more or less do, time consists of all time_xxxx datasets and time_2018 is the one of a specific year. I tried to make it as plain as possible but forgot the rename the variables in error message.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

dpb
dpb am 20 Jan. 2023
Presuming you don't try do do the logical addressing until the data array is defined so you don't have the initial error shown, then
ix=data(:,2)>3.3 & iswithin(data(:,3),235,275);
figure();
plot(time,data(:,2));
hold on;
plot(time(ix),data(ix,2),'r.');
hold off
should work fine; note you don't need find here, just use the logical array.
iswithin is my "syntax sugar" utility to reduce the clutter of compound test expressions -- it's quite handy in making things like the above more legible and particularly helpful in expressions.
function flg=iswithin(x,lo,hi)
% returns T for values within range of input
% SYNTAX:
% [log] = iswithin(x,lo,hi)
% returns T for x between lo and hi values, inclusive
flg= (x>=lo) & (x<=hi);
end
>>
One has to presume there's a disconnect owing to an error of some sort such as that shown in the posting that the indexing array being used is out of synch with the actual data array at the time it is being used.
Without the data and complete code snippet that reproduces the error we can't debug that type of an issue remotely...
  3 Kommentare
dpb
dpb am 20 Jan. 2023
Yes, although my utility function goes back to nearly ver 3.1...long before isbetween came into being.
S
S am 21 Jan. 2023
Thanks a lot! The iswithin function, or as you call it 'syntax sugar' utility function, implemented the statement on the data in the correct way!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 20 Jan. 2023
Your data variable has 157824 rows but your time variable only has 52560 rows . You cannot associate each row with a particular time because you have more rows than times.
If you wanted to extract only the first 52560 rows then you could do that.
num_to_use = min(size(data,1), size(time,1));
A3 = find(data(1:num_to_use,2)>3.3 & data(num_to_use,3)>235 & data(1:num_to_use,3)<275);
plot(time(1:num_to_use), data(1:num_to_use,2));
hold on
plot(time(A3), data(A3,2),'r.');
hold off
Or you could use a new facility:
num_to_use = min(size(data,1), size(time,1));
A3 = find(data(1:num_to_use,2)>3.3 & data(num_to_use,3)>235 & data(1:num_to_use,3)<275);
plot(time(1:num_to_use), data(1:num_to_use,2), '.', 'MarkerIndices', A3, 'MarkerFaceColor', 'r');
My system is busy at the moment so I cannot double check whether you would need MarkerEdgeColor instead of MarkerFaceColor for the '.' marker -- some markers use only one of the two properties, other markers use both properties.
  2 Kommentare
S
S am 21 Jan. 2023
This solution didn't work as I hoped it would, mainly because of the statement that I need to use. For my results it is important that the statement is executed correctly, it needs to be inbetween 235 and 275. Using these lines of codes, it also returns values that are outside of the range 235<data(:,3)<275 if the first conditions is met (data:,2)>3.3
Anyhow, thnx for helping me!
Walter Roberson
Walter Roberson am 22 Jan. 2023
num_to_use = min(size(data,1), size(time,1));
A3 = find(data(1:num_to_use,2)>3.3 & data(1:num_to_use,3)>235 & data(1:num_to_use,3)<275);
plot(time(1:num_to_use), data(1:num_to_use,2));
hold on
plot(time(A3), data(A3,2),'r.');
hold off
or
num_to_use = min(size(data,1), size(time,1));
A3 = find(data(1:num_to_use,2)>3.3 & data(1:num_to_use,3)>235 & data(1:num_to_use,3)<275);
plot(time(1:num_to_use), data(1:num_to_use,2), '.', 'MarkerIndices', A3, 'MarkerFaceColor', 'r');

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Plot Customization finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by