Extract data from a struct matrix and plot data with condition
23 Jul 2023
1 Answer
3 Views (30 days)
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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.
Accepted Answer
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

13 Comments
Jorge Luis Paredes Estacio
on 23 Jul 2023
Moved: Stephen23
on 23 Jul 2023
Thank you for your answer. How could it be automatized in the case that the number of iterations (n_i) are higher than 4 and these results may generate consecutive plots (higher than 2 plots as shown in the previous example) and plotting the number of iterations found among the different structure matrices (it bould be more than two iteration results). Thank you for your answer.
Voss
on 23 Jul 2023
If I understand your questions, I believe the code in my answer already does all of those things. Please try it and see if the plots are as expected.
Jorge Luis Paredes Estacio
on 23 Jul 2023
It works perfectly. Thank you very much. What I meant was to split the first and second plot (as shown below) with different colours as group of points in order to identify which group of points come from the first iteration, the second iteration, the third iteration, the fourth iteration, and so on (depending of the number of iterations). The group of points belonging to each group of iteration are plotted with different colours. Also, the legend can be adapted to identify the different points from the different group iterations they come from.
plot(A(ii).results(idx,1),A(ii).results(idx,2),'bo'); % plot 2nd column in blue (this blue colour is splitted into different colours to identify which group of point comes fro the first, the second, the third, and so on)
plot(A(ii).results(idx,1),A(ii).results(idx,3),'ro'); % plot 3rd column in red (this red colour is splitted into different colours to identify which group of point comes fro the first, the second, the third, and so on)
Thank you for your support
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});
Something like this would include a legend and allow all plotted lines to have different colors:
legend_names = {};
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 both columns at once, and leave the colors unspecified,
% to let MATLAB pick a new one for each line
plot(A(ii).results(idx,1),A(ii).results(idx,[2 3]),'o');
legend_names(:,end+1) = { ...
sprintf('Iteration %d, Amplitude w/Spikes',ii); sprintf('Iteration %d, Corrected Amplitude',ii) ...
};
end
legend(legend_names(:));

Or, if you want the line colors to be determined by iteration number only (i.e., lines from column 2 and column 3 in a given iteration would have the same color), then something like this:
figure();
legend_names = {};
colors = 'brkcmgy'; % need to specify at least as many colors as you have iterations
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 3]),['o' colors(ii)]);
legend_names(:,end+1) = { ...
sprintf('Iteration %d, Amplitude w/Spikes',ii); sprintf('Iteration %d, Corrected Amplitude',ii) ...
};
end
legend(legend_names(:));

Jorge Luis Paredes Estacio
on 23 Jul 2023
Brilliant!. Thank you very much. I really appreciate your help.
Voss
on 23 Jul 2023
You're welcome!
Jorge Luis Paredes Estacio
on 24 Jul 2023
One question,
this matrix of colors
colors = 'brkcmgy'
can be defined as different matrices as RGB Triplet using different "MarkerEdgeColor" and "MarkerFaceColor" as well. Thank you.
Jorge Luis Paredes Estacio
on 24 Jul 2023
For the case, I would like to add two plots before and then plot only the first results values like this:
plot 1 (Raw Acc. EW Component)
plot 2 (PGA)
plot(TIME(spikes_X(ii).results(idx,1)),spikes_X(ii).results(idx,2),['o' colors(ii)]);
legend_names(:,end+1) = { 'Raw Acc. EW Component', 'PGA', sprintf('Iteration %d, Amplitude w/Spikes',ii)}; %here I add the name of the two first plots.
I tried this but the matrix generated does not allow me to use the legend with the two additional names I added at the beginning ('Raw Acc. EW Component', 'PGA').
Thank you for your support.
Voss
on 24 Jul 2023
"[colors] can be defined as different matrices as RGB Triplet using different "MarkerEdgeColor" and "MarkerFaceColor" as well."
That's correct. You can specify colors as RGB triplets or using the short names ('b', 'r', 'g', 'm', etc.). I used the short names since it made the code more compact/easier to follow. The code below uses RGB triplets instead.
"I would like to add two plots before and then plot only the first results values"
See below for an example of that. Basically, you just have to plot before the loop and also specify the legend names of the first two lines before the loop (not inside the loop) as well.
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});
% some data to plot first:
raw_data = 1.25*cos((1:1000)/50)-0.25;
pga_data = 1.25*sin((1:1000)/50)-0.25;
plot(raw_data);
hold on
plot(pga_data);
colors = [0 1 0; 1 0 1; 1 0 0; 0 0 1]; % This is green, magenta, red, blue. You can use whatever colors you like here.
legend_names = {'Raw Acc. EW Component' 'PGA'};
% if you want just the first iteration plotted, remove the for and set
% ii = 1 (or use A(1) throughout), or just do for ii = 1
% for ii = 1:numel(A)
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), ...
'Marker','o', ...
'LineStyle','none', ...
'Color',colors(ii,:), ...
'MarkerFaceColor',colors(ii,:), ...
'MarkerEdgeColor',colors(ii,:));
legend_names(end+1) = { ...
sprintf('Iteration %d, Amplitude w/Spikes',ii) ...
};
end
legend(legend_names);

Jorge Luis Paredes Estacio
on 24 Jul 2023
Thank you so much.!!. It worked perfectly.
Voss
on 24 Jul 2023
You're welcome!
Jorge Luis Paredes Estacio
on 24 Jul 2023
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
I had to google, "harmonized matrix", which immediately lead me to your other question about this, which I have now answered. Take a look:
More Answers (0)
Categories
Find more on Line Plots in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)