create csv file with user input and current date as name

7 views (last 30 days)
Hello everyone
I want to create a csv file in desired location with name that include user input and current date.
I manage to create a csv file with the user input and the current date but I'm unable to save it in desired location
SN = input('Please enter the Serial Number ', 's');
fileName = sprintf('freq_res_data_%s_%s.csv',SN, datestr(now,'dd_mm_yy_HHMMSS'));
csvwrite(fileName,out);
I tried to run add the full direction (d:\tom\) but it doesnt work
I tried only on d:\ and it still deson't work
SN = input('Please enter the Serial Number ', 's');
fileName = sprintf('d:\tom\freq_res_data_%s_%s.csv',SN, datestr(now,'dd_mm_yy_HHMMSS'));
csvwrite(fileName,out);
tom folder is exist
how can I save in any other directory then the current folder?

Answers (2)

Benjamin Thompson
Benjamin Thompson on 11 May 2022
Use sprintf to generate a local string variable. Use a \\ sequence to include a single \ in the string. Then pass the string as a filename to csvwrite.
» SN = '2'
SN =
'2'
» fileName = sprintf('d:\\tom\\freq_res_data_%s_%s.csv',SN, datestr(now,'dd_mm_yy_HHMMSS'))
fileName =
'd:\tom\freq_res_data_2_11_05_22_075347.csv'
  2 Comments
Tamir Yakobov
Tamir Yakobov on 11 May 2022
thanks,
its working in d:\ and d:\tom directories but it's not working on c:\
any idea why?
Benjamin Thompson
Benjamin Thompson on 11 May 2022
Please post the code and any errors you are getting from MATLAB. Remove semicolons after statements so you can see return results.

Sign in to comment.


Tamir Yakobov
Tamir Yakobov on 11 May 2022
>> test
Please enter the Serial Number 777
SN = 777
fileName = c:\freq_res_data_777_11_05_22_171425.csv
error: dlmwrite: Invalid argument
error: called from
dlmwrite at line 177 column 5
csvwrite at line 44 column 3
test at line 22 column 1
>> test
Please enter the Serial Number 777
SN = 777
fileName = d:\freq_res_data_777_11_05_22_171444.csv
can it be a permission issue?

Tags

Community Treasure Hunt

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

Start Hunting!