Difference between revisions of "Program Examples:Octave interface"
(Example of an Octave function that reads a softMC record file) |
(adding comments) |
||
Line 3: | Line 3: | ||
<syntaxhighlight lang="matlab"> | <syntaxhighlight lang="matlab"> | ||
function [rec, vars] = MCplot(filepath) | function [rec, vars] = MCplot(filepath) | ||
− | fid = fopen(filepath,'r'); | + | fid = fopen(filepath,'r'); % open file and save its id to fid |
− | n = fread(fid,1,'int32'); | + | n = fread(fid,1,'int32'); % n-> raw number of columns |
− | m = fread(fid,1,'int32'); | + | n = mod(n,16); % n -> fix the number of columns |
+ | m = fread(fid,1,'int32'); % m -> number of rows | ||
gap = fread(fid,1,'int32'); | gap = fread(fid,1,'int32'); | ||
vlen= fread(fid,1,'int32'); | vlen= fread(fid,1,'int32'); | ||
− | vars = fgets(fid,vlen); | + | vars = fgets(fid,vlen); % string stating the recorded variables |
− | |||
rec = zeros(m,n); | rec = zeros(m,n); | ||
for i = 1:m | for i = 1:m | ||
for j = 1:n | for j = 1:n | ||
− | + | rec(i,j) = fread(fid,1,'double'); | |
− | |||
end | end | ||
end | end |
Revision as of 13:44, 8 March 2017
Example of an Octave function that reads a softMC record file. (Octave is an open source software with a similar syntax to Matlab)
function [rec, vars] = MCplot(filepath)
fid = fopen(filepath,'r'); % open file and save its id to fid
n = fread(fid,1,'int32'); % n-> raw number of columns
n = mod(n,16); % n -> fix the number of columns
m = fread(fid,1,'int32'); % m -> number of rows
gap = fread(fid,1,'int32');
vlen= fread(fid,1,'int32');
vars = fgets(fid,vlen); % string stating the recorded variables
rec = zeros(m,n);
for i = 1:m
for j = 1:n
rec(i,j) = fread(fid,1,'double');
end
end
fclose(fid);
end
The returned parameters are 'rec' and 'vars', were 'vars' is a string stating the recorded variables and 'rec' is the data of these variables ordered in a matrix such that var_1 -> rec(:,1), var_2 -> rec(:,2) ... var_n -> rec(:,n).
Example for plotting 2D data:
[rec, vars] = MCplot('C:\SoftMC\Projects\test.rec')
plot(rec(:,1),rec(:,2));
Example for plotting 3D data as a curve:
[rec, vars] = MCplot('C:\SoftMC\Projects\test.rec')
plot3(rec(:,1),rec(:,2),rec(:,3));