Presentation is loading. Please wait.

Presentation is loading. Please wait.

Note on Indexing of Array Elements

Similar presentations


Presentation on theme: "Note on Indexing of Array Elements"— Presentation transcript:

1 Note on Indexing of Array Elements
Indexing in MATLAB % No declarations % are necessary for i = [1:20] myarray( i ) = 0.0; end Indexing in C/C++ float myarray[20]; int i; for (i = 0; i < 20; i++) { myarray[ i ] = 0.0; }

2 Relational Operators in Matlab
Relational operators are used to compare two numbers or two arrays The inbuilt relational operators in Matlab are: Greater than… > Smaller than… < Equal to… == …What was the purpose of a single equals sign? Greater or equal… >= Smaller or equal… <=

3 Logical Operators in Matlab
When doing logical operations in Matlab, 0 always stands for false and everything else is true. Conventionally 1 is used for true statements. Logical operators in Matlab are: And… & Or… | Not… ~

4 Matlab Path A “path” is a string that specifies how to find a resource located in a file system (ex: C:\Matlab_workshop\module_1 ) The Matlab Path is a list of many paths to be searched in succession whenever Matlab needs to find something Starts with a basic set of paths Can be extended by the user (using File/Set Path…) If Matlab tells you that it cannot locate a file, perhaps you have not specified a path to find it…

5 Inputting Data from Open Files
Data that has been read in as a string using fgets can be parsed using the function sscanf. The syntax to input a line and then extract and discard three columns from the string and save the fourth column of floating point data in an element in the array myarray would be: line = fgets(infile_id); myarray(k)=sscanf(line,'%*s%*s%*s%f');

6 Loading Data Let’s clear the workspace so we can tell if anything was loaded: >> clear all >> whos Load file ex1.mat: >> help load >> load ex1

7 Reading Data from Files
The load command does not always succeed in reading an input file. It only works when all lines in the file have the same ASCII format. Files with header material do not qualify, nor do binary files. ASCII files that cannot be input with the load function can be opened and input with MATLAB functions that are similar to C language functions we have been using. The MATLAB functions include fopen, fgets, fscanf, and sscanf.

8 Opening Data files The command to open a file for input is:
infile_id = fopen('filename','r'); The command to open a file for output is: outfile_id = fopen('filename','w'); r-for read w- for write

9 Saving Data >> help save
Let’s save both t and y1 in file ex1.mat: >> save ex1 t y >> dir

10 Try this: x = 0:.1:1; y = [x; exp(x)]; fid = fopen('exp.txt','w'); fprintf(fid,'%6.2f %12.8f\n',y); fclose(fid)

11 Printing Data to Files When a file has been opened for output, the command to output one line to the file as a string is: fprintf(outfile_id,'%s',line); Lines of other data can be output by inserting the appropriate format string and variable names. For example: fprintf(outfile_id,'%f%f\n',a,b);

12 Printing to the Screen Data can also be printed to the screen with the fprintf function. To do so, we omit the file idenification (outfile_id). For instance, to print a line of text to the screen we could use: fprintf('%s',line) and to print other data we might use: fprintf('%f %f\n', a , b)

13 Manual Definition of t Create with brackets [ ]
Separate elements inside with spaces or commas >> t = [0, 0.1, 0.2, …] Access individual elements with integer index enclosed in parentheses ( ) >> t(1), t(2), t(end) Indices start at 1.

14 Checking Workspace Variables are defined in workspace
Can list all variables in workspace using the command whos Can clear any or all variables: >> clear t >> clear all

15 The programming environment
Executable m-files are located in the current working directory or somewhere on the search path. The function/variable duality causes Matlab to search for an identifier according to the scheme 1. variable in current workspace 2. built-in variable 3. built-in m-file 4. m-file in current directory 5. m-file on search path

16 Simple Examples %Example1.m X=5; Y=3; Z=3X^2+5; Z
% character denotes the start of a comment lines that end with a “;” are simply run with out displaying result lines that do not end with a “;” are run and results displayed on command window

17 .m Files Once your program has been saved as a .m file it can be run by either pressing F5 in the editor or by typing the file name in the command window Other .m files maybe be run from within different .m files

18 RLC Series .m File %Example2.m - RLC w=linspace(1,40000,1000);
v=10./(10+(1./(j.*w.*3.*10^-6))+(j.*w.*0.01)); plot(w,abs(v)); j is intrinsically unless its redefined

19 RLC series result

20 RLC series result

21 Exercise The circuit shown in Figure 1.0 is describe by the following set of node voltage equations when the input is a steady-state sinusoidal waveform. Determine the voltage at each of the nodes.


Download ppt "Note on Indexing of Array Elements"

Similar presentations


Ads by Google