MATLAB Input and Output - Summary Notes
1. Introduction to Input/Output
- Input Sources:
- User typing a value.
- A file on the hard disk.
- Output Destinations:
- Screen (MATLAB command window).
- A file on the hard disk.
2. Input Functions
- Using 'input' function:
x = input(prompt)
str = input(prompt, 's')
Example:
prompt = 'What is the original value?';
x = input(prompt);
y = x * 10;
3. Creating Dialog Boxes
- Using 'inputdlg' function to create input dialogs.
answer = inputdlg(prompt, dlgtitle, dims, definput, opts)
Example:
prompt = {'Enter matrix size:', 'Enter colormap name:'};
dlgtitle = 'Input';
dims = [1 35];
definput = {'20', 'hsv'};
answer = inputdlg(prompt, dlgtitle, dims, definput);
4. Output Statements
- Using 'disp' for simple output:
disp('Hello, MATLAB!');
disp(42);
- Using 'fprintf' for formatted output:
fprintf('Value: %6.2f\n', 3.14159);
Conclusion
- Use 'input' or 'inputdlg' for user input.
- Use 'disp' for simple output and 'fprintf' for formatted output.
- Use 'disp' for matrices as 'fprintf' can be awkward.