Difference between revisions of "Program Examples:Octave interface"

From SoftMC-Wiki
Jump to: navigation, search
(Example of an Octave function that reads a softMC record file)
 
 
(2 intermediate revisions by one other user not shown)
Line 1: Line 1:
 +
{{Languages|Program_Examples:Octave_interface}}
 
Example of an Octave function that reads a softMC record file. (Octave is an open source software with a similar syntax to Matlab)
 
Example of an Octave function that reads a softMC record file. (Octave is an open source software with a similar syntax to Matlab)
  
 
<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);       % vars -> string stating the recorded variables
    n = mod(n,16);
 
 
     rec = zeros(m,n);
 
     rec = zeros(m,n);
 
   
 
   
 
     for i = 1:m
 
     for i = 1:m
 
       for j = 1:n
 
       for j = 1:n
       tmp = fread(fid,1,'double');
+
       rec(i,j) = fread(fid,1,'double');
      rec(i,j) = tmp;
 
 
     end
 
     end
 
     end
 
     end

Latest revision as of 07:35, 17 July 2017

Language: English  • 中文(简体)‎

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);       % vars -> 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));