Extract data from a struct matrix and plot data with condition

7 views (last 30 days)
Commented: Voss on 24 Jul 2023
Hello, I obtain a struct matrix than contains different results according to the number of iterations that was performed. Let us say that the number of iteration is 4 (n_i=4). So I obtain the "results" values in a structured matrix (A):
A(1x4 struct)
Inside the matrix, I have a single column with 4 rows that has the "results".
As an example I provide the folling results inside the matrix A:
7x3 double
8x3 double
-1
-1
As we can see, the first two rows only have the data that will be used for plotting purposes as they come from the first and second iteration. The other two rows with "-1" indicates that only two iterations were needed and the third and fourth iterations are not neccesary for plotting as they do not provide results.
Now, inside each structure (the first-7x3 and second row-8x3 for this example), the first column contains the indexes of the location of each value and only the indexes different from 0 will be used for plotting (as they do not provide any additional data in the other columns, see second matrix in the example below) . The values of the indexes from the first column will match the position to the data located in the other two columns (the second column is the current amplitude that has spike issues and the third column is the corrected amplitude of the signal where spikes were eliminated according to the neighbouring average). The information obtained from the second and third column will plot on top on the entire signal with spikes and withouth spikes (which is not addressed here but it contains the total number of indexes and amplitudes).
The plot should be automatized, so only the matrices that contains data will be plotted indicating that each point comes from either first or second iteration.
The matrix 7x3 can have the following structure
158 -0.573800000000000 0.144935922330097
171 -0.515800000000000 -0.145098543689320
228 -1.22680000000000 -0.439054854368932
347 -0.636800000000000 -0.00694077669902918
352 0.903200000000000 0.390577901934952
358 -0.632800000000000 -0.0447915471908687
368 -0.653800000000000 -0.209547572815534
and the matrix 8x3 can have the following struncture
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
0 0 0
452 -0.618459223300971 -0.325464299874279
855 0.310200000000000 0.0439810936907452
I would appreciate any help.

Voss on 23 Jul 2023
M7 = [ ...
158 -0.573800000000000 0.144935922330097; ...
171 -0.515800000000000 -0.145098543689320; ...
228 -1.22680000000000 -0.439054854368932; ...
347 -0.636800000000000 -0.00694077669902918; ...
352 0.903200000000000 0.390577901934952; ...
358 -0.632800000000000 -0.0447915471908687; ...
368 -0.653800000000000 -0.209547572815534];
M8 = [ ...
0 0 0; ...
0 0 0; ...
0 0 0; ...
0 0 0; ...
0 0 0; ...
0 0 0; ...
452 -0.618459223300971 -0.325464299874279; ...
855 0.310200000000000 0.0439810936907452];
A = struct('results',{M7 M8 -1 -1})
A = 1×4 struct array with fields:
results
hold on
for ii = 1:numel(A)
if isequal(A(ii).results,-1) % skip elements of A where results is -1
continue
end
idx = A(ii).results(:,1) ~= 0; % only plot values where the 1st column is not zero
plot(A(ii).results(idx,1),A(ii).results(idx,2),'bo'); % plot 2nd column in blue
plot(A(ii).results(idx,1),A(ii).results(idx,3),'ro'); % plot 3rd column in red
end
One more thing please. If I would like to generate a harmonized matrix based on the structure matrix as mentioned before dependent on the number of iterations:
A = struct('results',{M7 M8 -1 -1})
and get the following matrix
A_total=
158 -0.573800000000000 0.144935922330097; ...
171 -0.515800000000000 -0.145098543689320; ...
228 -1.22680000000000 -0.439054854368932; ...
347 -0.636800000000000 -0.00694077669902918; ...
352 0.903200000000000 0.390577901934952; ...
358 -0.632800000000000 -0.0447915471908687; ...
368 -0.653800000000000 -0.209547572815534;...
452 -0.618459223300971 -0.325464299874279; ...
855 0.310200000000000 0.0439810936907452];
How shoould I proceed. Thank you.
Voss on 24 Jul 2023

Categories

Find more on Line Plots in Help Center and File Exchange

R2021a

Community Treasure Hunt

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

Start Hunting!