Basic I/O


User (Command Window) Input / Output

Basic commands:

Reading in a scalar

prompt = 'What is the current temperature (in degrees Fahrenheit)? ';

currTempF = input(prompt);

currTempC = (5/9) * (currTempF - 32);
disp(['The current temperature is ', num2str(currTempC), ' Celsius'])

 

Reading in a matrix

grades = input('Enter the scores: ');

disp(['Average score = ', num2str(sum(grades)/length(grades))])

 

 

 

 

Reading in a string

outFile = input('Enter filename: ', 's');

disp(['About to open ', outFile])

 

 

 

 

 

 

 

 

 



Giving a menu of options


choice = menu('Choose color:', 'red', 'blue', 'black', 'green');

colorOpts = ['r', 'b', 'k', 'g'];
plot(x, y, colorOpts(choice))

 

 

 

 

 

 

 

 


File Input / Output

File format (for basic file I/O):

 

 

Reading in from a text file

varName = load( filename )

 

 

 

 

 

 

Saving to a text file

save( filename, varName, '-ascii' )