MATLAB Answers

What is the easiest way to create a textbox?

17 views (last 30 days)
Travis Schauer
Travis Schauer on 17 Apr 2018
Answered: njj1 on 18 Apr 2018
I am attempting to create a textbox on my plot, but it has to include all of the information displayed in my my fprintf statements and has to be in the top right.
Here is my fprintf:
fprintf('\nProducing %0.0f %s bats a week for %0.0f weeks = %0.0f total bats\n',num_bats(1,1),material_string,num_weeks(1,1),total_bats)
fprintf(' Selling Price per bat: $%0.2f\n',selling_price(1,1))
fprintf(' Total Variable Cost per bat: $%0.2f\n',variable_cost)
fprintf(' Fixed Cost of upgrade: $%0.2f\n',upgrade_cost)
fprintf(' Profit: $%0.1e\n',profit(plength))
fprintf('Breakeven Point: %0.0f bat(s)\n\n',breakeven)
I am not familiar with the annotation function, so any help would be great!

Answers (1)

njj1 on 18 Apr 2018

The annotation command is quite useful and can save time for sure. Unfortunately, it does not have any 'top right' commands, so you have to know/estimate the location for the textbook within your figure. The call to annotation might look like:

T = annotation('textbox','string',{sprintf(Producing %0.0f %s bats a week for %0.0f weeks = %0.0f total bats',num_bats(1,1),material_string,num_weeks(1,1),total_bats), ...
                  sprintf(('Selling Price per bat:  $%0.2f',selling_price(1,1)),...
                  sprintf('Total Variable Cost per bat:  $%0.2f',variable_cost),...
                  sprintf('Fixed Cost of upgrade:  $%0.2f',upgrade_cost),... 
                  sprintf('Profit:  $%0.1e',profit(plength)), ...
                  sprintf('Breakeven Point:  %0.0f bat(s)',breakeven)});
T.Position = [x_start y_start x_length_normalized y_length_normalized]; %text box position and size
T.LineStyle = '-'; %this is the type of line you would like around the box

By putting all the 'sprintf' statements in a cell, we can force each entry into a new line, so we don't have to use \n anymore. You can also call BackgroundColor, FontName, FontSize, etc. as parts of the structure 'T'.

Community Treasure Hunt

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

Start Hunting!