MATLAB Programming
Basic MATLAB Concepts
Saving and loading a MAT-file
file > set path... > add folder...
save filename.mat or
save filename.fig
- save - saves data to files, *.mat by default
- uisave - includes user interface
- hgsave - saves figures to files, *.fig by default
- diary [filename] - saves all the text input in the command window to a text file
One way is to use the "file" menu. To open a .m file click "open",
whereas to import data from a data file select "import data..." and
follow the wizard's instructions.
To open an .m file, you can use file -> open, or type
>>open filename.mat
MATLAB Command Prompt
Calculator
>> 3503-3215 ans = 288
>> 288/10 ans = 28.8
>> 28.8 * 15.4 ans = 443.5200
>> distance = 3503-3215 distance = 288 >> mileage = distance/10 mileage = 28.8000 >> projected_distance = mileage * 15.4 projected_distance = 443.5200
Reading and Writing data from/to a .mat file
save command is used to save workspace data to a file.
>> save('mySave.mat') >> save(fullfile(pwd, 'mySave.mat'))
Save just the variables myData1 and myData2 to mySave.mat.
>> save('mySave.mat', 'myData1', 'myData2')
Save all myData variables to mySave.mat.
>> save('mySave.mat', 'myData*')
Save all myData variables to a mySave.mat file compatible with version 6 of MATLAB.
>> save('mySave.mat', 'myData*', '-v6')
Load all variables from the file mySave.mat into the current workspace.
>> load('mySave.mat') >> load(fullfile(pwd, 'mySave.mat'))
Load all myData variables.
>> load('mySave.mat', 'myData*')
Get a cell array of variables in saved file.
>> whos('-file', 'mySave.mat')
MATLAB makes it easy to read from an Excel spreadsheet.
It has the built in command "xlsread". To use the xlsread function
use the syntax:
>>g=xlsread('filename');
To write data to an .xls the procedure is very similar. The xlswrite command below
creates a spreadsheet called filename.xls in the current directory from
the variable g:
>> xlswrite('filename',g);
Reading and Writing from and to other text files
If a file is not an excel spreadsheet, it can still be read using "load" function:>> load newfile.txt
Declaring Strings
Besides numbers, MATLAB can also manipulate strings.
They should be enclosed in single quotes:
>> fstring = 'hello'
fstring =
hello
>> fstring = 'you''re' fstring = you're
An important thing to remember about strings is that MATLAB treats them as array of characters. To see this, try executing the following code:
>> fstring = 'hello'; >> class(fstring) ans = char
Attempting to perform arithmetic operations on character arrays
converts them into doubles.
>> fstring2 = 'world'; >> fstring + fstring2 ans = 223 212 222 216 211
These values are obtained by using the 'double' function to turn the
array into an array of doubles.
>> double(fstring) ans = 104 101 108 108 111
Displaying values of string variables
>> fstring = 'hello'; >> display( [ fstring 'world'] ) helloworld
>> fstring = ['you' char(39) 're'] fstring = you're
>> strCell = {'A', 'B'}; >> strcat(strCell, '_'); ans = A_ B_
>> X = 9.2 >> fprintf(1, '%1.3f\n', X); 9.200
>> X = 9.2 >> fprintf(1, 'The value of X is %1.3f meters per second \n', X); The value of X is 9.200 meters per second
>> strCell = {'Aa', 'AA'}; >> strmatch('A', strCell); ans = 1, 2 >> strmatch('A', strCell, 'exact'); ans = [] >> strmatch('Aa', strCell, 'exact'); ans = 1
>> strCell = {'Aa', 'AA'}; >> strfind(strCell, 'A'); ans = % answer is a cell array with two elements (same size as strCell): 1 % Index of the beginning of string "A" in the first cell 1 2 % Index of each instance of the beginning of string "A" in the second cell >> strfind(strCell, 'a'); ans = 2 [] % 'a' is not found
>> string1 = 'a'; >> strcmp(string1, 'a') ans = 1 >> strcmp(string1, 'A') ans = 0
this page is so helpful ...really thanks
ReplyDelete