필터 지우기
필터 지우기

Error message while using if statement

조회 수: 9 (최근 30일)
Noah Wilson
Noah Wilson 2019년 6월 12일
댓글: dpb 2019년 6월 12일
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.
  댓글 수: 1
dpb
dpb 2019년 6월 12일
Other respondent answered the cause for the syntax error; I'll just note that
fn=fullfile('Rawdata');
is a "do nothing" statement as written-- fullfile only does something when given a qualifying path in addition to a filename string. Whether this is the intent or not is not known but is the effect...

댓글을 달려면 로그인하십시오.

채택된 답변

Prasanth Sikakollu
Prasanth Sikakollu 2019년 6월 12일
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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 File Operations에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by