Geoscience Reference
In-Depth Information
control character \n denotes a new line at er each line of three numbers.
fid = fopen('geochem_formatted.txt','w');
fprintf(fid,'%u %6.4f %6.4f\n',data);
fclose(fid);
We can view the contents of the i le by typing
edit geochem_formatted.txt
which opens the i le geochem_formatted.txt
101 0.3657 0.0636
102 0.2208 0.1135
103 0.5353 0.5191
104 0.5009 0.5216
105 0.5415 NaN
106 0.5010 NaN
in the MATLAB Editor. h e format of the data is as expected.
2.7 Control Flow
Control l ow in computer science helps to control the order in which computer
code is evaluated. h e most important kinds of control l ow statements are
count-controlled loops such as for loops and conditional statements such as
if-then constructs. Since in this topic we do not deal with the programming
capabilities of MATLAB in any depth, the following introduction to the
basics of control l ow is rather brief and omits certain important aspects of
ei cient programming, such as the pre-allocation of memory prior to using
for loops, and instructions on how the use of for loops can be avoided by
vectorization of the MATLAB code. h is introduction is instead limited to
the two most important kinds of control l ow statements: the aforementioned
for loops and the if-then constructs. Readers interested in MATLAB as a
programming environment are advised to read the more detailed chapters
on control l ow in the MATLAB documentation (MathWorks 2014a andc).
h e for loops, as the i rst example of a MATLAB language statement,
execute a series of commands between for and end a specii ed number of
times. As an example we use such a loop to multiply the elements of an array
A by 10 , round the result to the nearest integer, and store the result in B .
clear
rng(0)
A = rand(10,1)
for i = 1 : 10
B(i,1) = round(10 * A(i));
Search WWH ::




Custom Search