getting the information from the figure

I have a code consisting of 4 requirements and their respective 3 variants.
It means that for each requirements, there are 3 variants assigned. and the final combination is 3^4, which is 81 total combinations.
I have all the combinations in the figure with blue dots for the tradespace study. When i click any of the points out of the total plotted points in the figure, it is just showing the x and y coordinates.
I want to know that if click on any of the points in the figure, will it going to reflect the combination architecture from the code?
How can i know that which point represent which combination out of 81 total combinations?
I am attching the figure in this thread and also the code to have the proper idea.
Any help in this area would be much appreciated and for me it is much needed.
Thanking you guys in advance.
%Define architecture decisions
arch_deci = ["a";"b";"c";"d";]
%Define architecture decision options
arch_deci_options = [
"HPP" "PA" "EPP" ;
"HLS" "ELS" "PC" ;
"HFS" "FA" "EFS" ;
"MINT" "ELMINT" "PR" ;
]
%Define relative costs
arch_deci_cost = [
1.0 3.0 8.0 ;
6.0 4.0 1.0 ;
1.0 3.0 8.0 ;
3.0 8.0 0.0 ;
]
%Define relative utilities
arch_deci_util = [
6.17 3.83 5.00 ;
5.37 5.57 1.00 ;
6.17 3.83 5.00 ;
5.78 5.59 0.00 ;
]
%Define weighting factor costs
weight_cost = [5;5;5;5;]
%Calculation of Cost
sum_weight_cost = sum(weight_cost)
%Define weighting factors for utilities
weight_util = [1;1;1;1;]
%Calculation of utilities
sum_weight_util = sum(weight_util)
%Generate all possible architectures (without using any constraints on feasibility)
total_archs = 81;
n_arch=total_archs;
n_arch=0
archs = strings(total_archs,4);
for a=1:3
a_opt = arch_deci_options(1,a);
for b=1:3
b_opt = arch_deci_options(2,b);
for c=1:3
c_opt = arch_deci_options(3,c);
for d=1:3
d_opt = arch_deci_options(4,d);
n_arch = n_arch+1;
archs(n_arch,:) = [a_opt b_opt c_opt d_opt ];
end
end
end
end
n_arch % this number should match total_archs
% Calculate cost and util for each architecture
archs_cost = zeros(n_arch,1);
archs_util = zeros(n_arch,1);
for arch_idx = 1:n_arch
cost = 0;
util = 0;
for deci_idx = 1:4
deci_opt = archs(arch_idx,deci_idx);
[row,col] = find(arch_deci_options == deci_opt);
cost = cost + arch_deci_cost(row,col) * weight_cost(deci_idx,1);
util = util + arch_deci_util(row,col) * weight_util(deci_idx,1);
end
archs_cost(arch_idx,1) = cost/sum_weight_cost;
archs_util(arch_idx,1) = util/sum_weight_util;
end
%Find the Pareto frontier
pareto_frontier = zeros(5,3);
pareto_frontier_archs = strings(5,5);
frontier_idx = 0;
%Sort util in descending order (largest util in the top)
[archs_util_sorted,arch_idx] = sort(archs_util,'descend');
%Order the cost and archs as per the sorted util order
archs_cost_sorted = archs_cost(arch_idx,:);
archs_sorted = archs(arch_idx,:);
prev_min_cost = 255;
c_util = 255;
start = 1;
for util_idx = 2:n_arch
util = archs_util_sorted(util_idx,1);
if util == c_util
continue
end
temp_cost = archs_cost_sorted(start:util_idx,:);
[min_cost,temp_idx] = min(temp_cost);
if min_cost < prev_min_cost
prev_min_cost = min_cost;
%The index of the arch from arch_idx
pidx = (temp_idx - 1) + start;
frontier_pidx = arch_idx(pidx,:);
%Store the frontier point
frontier_idx = frontier_idx + 1;
pareto_frontier(frontier_idx,1) = frontier_pidx;
pareto_frontier(frontier_idx,2) = archs_cost_sorted(pidx,:);
pareto_frontier(frontier_idx,3) = archs_util_sorted(pidx,:);
%Store the frontier archs.
pareto_frontier_archs(frontier_idx,1) = frontier_pidx;
pareto_frontier_archs(frontier_idx,2:5) = archs(frontier_pidx,:);
end
%Restart
start = util_idx;
c_util = util;
end
Utopia = UtilityHighRange; % for plotting purposes add Utopia point
%Plot the tradespace and pareto frontier
plot(archs_cost,archs_util,'b.')
xlabel('Cost')
ylabel('Utility')
xlim([CostLowerRange CostHighRange])
ylim([UtilityLowerRange UtilityHighRange])
title('Trade Space Pareto')
hold on
plot(Utopia,'pg','LineWidth',5)
annotation('textarrow',[0.1657 0.1357],[0.8738 0.9238],'String','Utopia')
plot(pareto_frontier(1:frontier_idx,2), pareto_frontier(1:frontier_idx,3),'rs','LineWidth',3)
hold off

 Accepted Answer

BobH
BobH on 23 Apr 2020
This older question/answer suggests adding the 'Tag' property to the plot.
It seems you could use pidx and frontier_idx to find the right information to tag.

14 Comments

Could you please direct me to the step? As I am a newbie in this area and would likely to have the direction in the way of code so that i can try the method suggested by you.
I'm being limited on comment length. Using the link's example, plot each point separately, building the information you want displayed, and tagging the point with that info. Then define a callback function UpdateFcn. Then use the example's handler function and create the "txt" you want to show. I think you're trying to show the correct element from archs for each clicked point. Also add tagging to the pareto plot
%Plot the tradespace and pareto frontier
plot(archs_cost(1),archs_util(1),'b.','Tag',sprintf('ix %d\n%s',1, join(archs(1,:))) )
hold on % moved up from below
for n = 2:n_arch
plot(archs_cost(n),archs_util(n),'b.','Tag',sprintf('ix %d\n%s',1, join(archs(n,:))) )
end
datacursormode on
dcm = datacursormode(gcf);
set(dcm,'UpdateFcn',@myupdatefcn)
function txt = myupdatefcn(trash,event)
pos = get(event,'Position');
tagtxt = get(event.Target,'Tag');
txt = {tagtxt, ... % tag as provided during plot()
['X: ',num2str(pos(1))], ...
['Y: ',num2str(pos(2))]};
end
Thanks a ton for such a comprehensive answer and being so helpful.
I will try your method and see this and i think that it should work. And yes, I want to see that in figure, which point is representing which combination out of 81 total combination, after clicking on the point in the figure.
If this is what you meant with, "I think you're trying to show the correct element from archs for each clicked point.", then it should definetely work and if you have some other idea after the above combination point, I would be glad to recieve any input.
I will try and will let you know if this works.
Waiting for your answer and thank you.
one short help needed:
In that plot, I can access the data with their respective combinations, having the blue dot.
But I could not see the combination on the red highlighted dot (pareto_frontier in the above code) on the plot.
What should i do there?
The red dots are put there by a second plot() call, near the bottom of your code.
Use the 'Tag' approach on that plot as well (plot individual points, tag each one with a string) but using a join on (I think) pareto_frontier_archs(frontier_idx,2:5)
plot(pareto_frontier(1, 2), pareto_frontier(1, 3), 'rs', 'LineWidth', 3, 'Tag', sprintf('ix %d\n%s', 1, join(pareto_frontier_archs(1, :))))
hold on
for n = 2:frontier_idx
plot(pareto_frontier(n:frontier_idx, 2), pareto_frontier(n:frontier_idx,3), 'rs', 'LineWidth', 3, 'Tag', sprintf('ix %d\n%s', 1, join(pareto_frontier_archs(n, :))))
%plot(pareto_frontier(1:frontier_idx,2), pareto_frontier(1:frontier_idx,3),'rs','LineWidth',3)
end
datacursormode on
dcm = datacursormode(gcf);
set(dcm, 'UpdateFcn', @myupdatefcn)
hold off
I have come up with this above code and so far it is generating the data from the highlighted points as well.
Want to know if it is a right approach?
Yes, that is what I was thinking. Of course, all the information written to the tag by sprintf() is customizable by you, and similarly you can adjust the information written to 'txt' in the user function. What I provided was examples of things that were possible, not that they were necessary.
Because I check and double-check things, I would confirm that the values you save away at the time you "%Store the frontier archs" are the same values that pop up when you click on a red dot. You can remove the semicolon from this line and it will print the values saved to your command window, at the time you save them.
pareto_frontier_archs(frontier_idx,2:5) = archs(frontier_pidx,:) % no semicolon
Thank you very much. It helped me a lot and thank you once again.
Hi @BobH,
I am thinking that from the above solution of getting the values from the figure, How can I get the x and y coordinates values of the points?
It would be really helpful as I am calculating distance of the points and for that it is needed.
Thanking you in advance.
Are the coordinate values the same as the plot points? You could add new items to the sprintf used to record the Tag or handle clicking on the plot. See the examples in comments I posted on 23 Apr. I wonder if the X Y position in this 23 Apr comment is what you are looking for.
function txt = myupdatefcn(trash,event)
pos = get(event,'Position');
tagtxt = get(event.Target,'Tag');
txt = {tagtxt, ... % tag as provided during plot()
['X: ',num2str(pos(1))], ...
['Y: ',num2str(pos(2))]};
end
I have used this following:
for k = 1 : length(pareto_frontier(:, 2))
fprintf('x = %f, y = %f.\n', pareto_frontier(k, 2), pareto_frontier(k, 3));
end
Is it okay to use or do you have some other suggestion?
Oh, you are just getting the coordinates, and it's unrelated to the "clicking-on-the-plot" part of your question.
So your final plot uses pareto_frontier(:2) and pareto_frontier(:,3), and your loop is ok, but you might also consider using size instead of length to figure out how many elements there are
for k = 1:size(pareto_frontier,1) % first dimension of pareto_frontier
ohh ok. I try doing that too. And how can I export this coordinate values to the excel file using coomand, writematrix?
Any input in that?

Sign in to comment.

More Answers (1)

BobH
BobH on 20 May 2020
For recent versions of MATLAB (R2019a and after), the help on writematrix has useful examples that I think apply to your question.
If you have an older MATLAB, then assuming you have Windows, and assuming Microsoft Office is installed on your pc, you could use xlswrite. See https://www.mathworks.com/matlabcentral/answers/74322-export-data-from-matlab-to-excel
If you have new difficulties with writematrix, I suggest you create a fresh question to get more readers/helpers.

Categories

Find more on MATLAB in Help Center and File Exchange

Asked:

on 23 Apr 2020

Answered:

on 20 May 2020

Community Treasure Hunt

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

Start Hunting!