Matlab Sample 2

% % % % % % Some useful how-to's for Matlab % % % % % % % The following contains helpful codes for % 1. How to read/import numerical variables in .csv files, % 2. Vector and Matrices 1. myfilename = sprintf('./file_1.csv'); Var1 = (csvread(myfilename))'; % if file_1.csv at the current working directory contains % a variable data with only numerical values, this allows % Matlab to read the file and assign to Var1 % % % this codes read in multiple csv files % % % % if we have multiple files file_1.csv, file_2.csv ... to % file_I.csv to be read, the following is convenient Var = cell(I); for i = 1:I myfilename = sprintf('./file_%d.csv', i); Var{i} = (csvread(myfilename))'; end % Var{i} is variable i contained in file_i.csv % % % % % % % for more detail about formating strings, see % http://www.mathworks.com/help/matlab/matlab_prog/formatting-strings.html % http://www.mathworks.com/help/matlab/ref/sscanf.html 2. vector = linspace(0.1, 1, 5); % vector = % 0.1000 0.3250 0.5500 0.7750 1.0000 % linspace(left, right, n) generates n equally spaced points from left to right as a row vector % n is an integer and its default value is 100 mat = transpose(vector) * vector; % mat = % 0.0100 0.0325 0.0550 0.0775 0.1000 % 0.0325 0.1056 0.1788 0.2519 0.3250 % 0.0550 0.1788 0.3025 0.4263 0.5500 % 0.0775 0.2519 0.4263 0.6006 0.7750 % 0.1000 0.3250 0.5500 0.7750 1.0000 % % % % % % % More handy codes will continue to be added %