MATLAB: Error message while using if statement

create foldererrorifif statementinvalid syntax '='MATLAB

I am trying to create an if statement that will create a file "Rawdata" in my current directory but I also want it to check if the folder of that name already exists and only create the folder if it does not exist. Then I want to change my directory to that new folder and save a csv file to it.
The code I am using is as follows:
%Making new folder for raw data
fn = fullfile('Rawdata');
if
exist(fn, 'dir')
warning('This folder already exists')
else
mkdir(fn)
end
%Changing directory
cd('Rawdata');
%Writing new csv file to test folder
csvwrite('rawdata.csv',T3);
I am currently getting at error message right at the begining of the if statement saying "Invalid expression. Check for missing or extra characters." and then in the code is it telling me "Parse error. Usage might be invalid MATLAB syntax".
Any suggestions on what I am doing wrong or how to fix it would be greatly appreciated.

Best Answer

Syntax of if conditional:
if expression
statements
else
statements
end
There should be no newline character between 'if' and expression.
The following code should work.
if exist(fn, 'dir') % if expression
warning('This folder already exists') % Statements
else % else
mkdir(fn) %Statements
end