%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% read_writefile_demo.m
%
% purpose: demo how to read from and written to data in matlab
%
% written by tancy 05/08/2017
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% Writting data to a file
% save('filename', 'matrixvariablename')

mymat = rand(2,3)
save('testfile.mat', 'mymat')
save('/Users/tancy/Documents/testfile.mat', 'mymat') % save file to defined pwd

% Save two variables, where FILENAME is a variable:
savefile = 'pqfile.mat';
p = rand(1, 10);
q = ones(10);
save(savefile, 'p', 'q');

%% Reading from a file

clear all % clear all variables and data

% load mat files
load('testfile.mat')
whos % check variables in Workspace
load('/Users/tancy/Documents/testfile.mat') % load file from defined pwd
whos  % check variables in Workspace

%% write and read microsoft excel spreadsheet file
% num = xlsread(filename)
% num = xlsread(filename,sheet)
% num = xlsread(filename,xlRange)
% num = xlsread(filename,sheet,xlRange)

% creat an excel file
values = randi(3,3) % generate a 3 x 3 random number matrix
xlswrite('myExample.xlsx',values);
mynums = csvread('myExample.csv') % csv: comma-separated values
mynums = xlsread('myexcel.xlsx')


%% save figures

x = [2 4 7 2 4 5 2 5 1 4];
bar(x);
saveas(gcf,'Barchart.png')





















