Reading from a File in a While Loop:
Though in most languages the combination of a loop and an if statement would be essential to determine whether or not the elements in a vector are logical true, the MATLAB has built-in functions like any, all, and find to accomplish the tasks.
For illustration, the data from an experiment has been recorded in a file known as 'experd.dat'. The file has few numbers followed by a -99 and then more numbers, all on the similar line. The only data values which we are interested in, though, are those before the -99. The algorithm for the script will be as shown:
1. Read the data from file into the vector.
2. Generate a new vector variable newvec which has the data values only up to but not involving the -99.
3. Plot the new vector values, by using black o's.
For illustration, if the file has the following:
3.1 11 5.2 8.9 -99 4.4 62
the plot generate would look like figure as shown below:
For ease, we will suppose that the file format is as specified. The load will generate a vector with the name experd that contains the values from the file. As this is also a generic data we will omit the plot labels and titles for simplicity.