User-Controlled Input and Output in MATLAB
Objectives of Chapter 7
Demonstrate how to prompt the user for input in M-files.
Review and utilize output functions: disp, fprintf, and sprintf for formatted outputs.
Create and manage tables using the table function.
Apply graphical methods for user program input and output.
User-Controlled Input and Output
User Defined Input
Hard-coded values have previously been used in M-files.
The input function allows dynamic prompting for user input in M-files.
This allows for flexibility depending on user input each time the program runs.
Input Types
Acceptable data types:
Scalars
Arrays: Enter values inside square brackets.
Character strings: Enter within single quotes (' ').
Optionally specify string input by using ‘s’.
Practice Exercise
Engage in hands-on activities that reinforce the understanding of input types in MATLAB.
Output Options
Display Output with disp Function
Use the disp function to display array contents, without printing the variable name.
Capable of showing strings as well.
Combine multiple disp calls for structured output, though each will appear on a new line.
Example:
disp(['The values in the x array are: ', num2str(x)])
To include an apostrophe inside a string, double it (e.g., 'The moon''s gravity').
Formatted Output with fprintf Function
fprintf provides extensive control over how the output format is displayed.
Capable of combining text with numeric values and controlling precision and formatting.
Usage:
fprintf('format_specifications', variables);
Placeholders are important to format the output correctly:
%f: floating-point number,
%d: integer,
%s: string, etc.
Example of using formatted output:
fprintf('x = %5.2f
', x);
Formatted Output to Files
Open a writable file to output data using the fprintf function combined with a file identifier.
Automatically counts characters output to the specified file.
Using sprintf for String Storage
sprintf functions similarly to fprintf, but it stores the output string for use later in the program.
Useful for annotating graphs or other dynamic contents in your code.
Example of sprintf Use
```matlab
myString = sprintf('The current temperature is %d degrees', temperature);
---
## Table Creation
- The **table** function enables the combination of different data types (text and numeric) into a single table format.
- Syntax example:
matlab
table(variable1, variable2, …);
- All arguments must be column vectors for proper table formatting.
---
## Graphical Input Techniques
- **ginput** function allows users to click on a graph to retrieve **x-y ordered pairs**.
- Displays a floating crosshair during interaction.
- Example usage:
matlab
[x, y] = ginput(n);
- Retrieves **n** ordered pairs based on user clicks.
### Adding Text with gtext
- **gtext** allows placing text at a specified graph location, making interaction more intuitive.
- Usage example:
matlab
gtext('Label');
```
Importing and Exporting Data
MATLAB supports multiple data formats including
.txt,.xls, and.jpgfor reading and writing.The load command is suitable for
.mator.datfiles.Specialized write commands exist for Excel (like writematrix) which allow for saving matrices directly to an Excel file.
Debugging Tools in MATLAB
Essential tools include the Code Analyzer, Breakpoints, and Error Highlighting features to assist in finding bugs in code.
Debugging methods enhance the user's ability to fix code errors efficiently.
Common Debugging Tips
Remember to include formatting specifiers in fprintf; missing them often leads to silent failures without explicit errors.
Summary of Key Functions
Input Function: Facilitates user data entry dynamically.
disp Function: Quick display without modification of the output format.
fprintf Function: Advanced formatted printing capabilities.
sprintf Function: Stores formatted strings for later use.
ginput Function: Graphical input retrieval.
table Function: Creates structured outputs with differing data types.
Overall, this chapter emphasizes the importance of interactive input/output management in enhancing user experience with MATLAB applications.