Creating file names for save command

I have the following code:
file_dir = 'C:\Users\mydir';
datestamp=string(datetime('now','TimeZone','local','Format','yyyyMMdd_HHmmss'));
file_name=strcat(file_dir,'sps_',datestamp,'.mat');
save(file_name);
which returns the following error:
Error using save
Argument must contain a character vector.
Not sure how to fix this?

2 Kommentare

Also tried this modification with out success. Same error although it does appear to generate a character vector..
file_name=sscanf(strcat(file_dir,'sps_',datestamp,'.mat'),'%s');
Same error:
save('temp.mat', data);
Error using save
Must be a string scalar or character vector.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

KSSV
KSSV am 6 Okt. 2016
Bearbeitet: KSSV am 6 Okt. 2016

1 Stimme

You have to put some data in the file....
file_dir = pwd;
datestamp=string(datetime('now','TimeZone','local','Format','yyyyMMdd_HHmmss'));
file_name=strcat(file_dir,filesep,'sps_',datestamp,'.mat');
data = rand(10,10) ;
save(file_name,'data');

5 Kommentare

Paul Nel
Paul Nel am 6 Okt. 2016
Unfortunately this is not the problem. As I see it the problem seems to be with the data type of the file name which I struggle to convert to a character vector.
Yes it is a warning....
Try:
datestamp=char(datetime('now','TimeZone','local','Format','yyyyMMdd_HHmmss'));
instead of
datestamp=string(datetime('now','TimeZone','local','Format','yyyyMMdd_HHmmss'));
Paul Nel
Paul Nel am 6 Okt. 2016
Same result unfortunately
How about replacing:
file_name=strcat(file_dir,filesep,'sps_',datestamp,'.mat');
with
file_name=char(strcat(file_dir,filesep,'sps_',datestamp,'.mat'));
Jan
Jan am 6 Okt. 2016
Prefer fullfile to join path and file names.

Melden Sie sich an, um zu kommentieren.

Thorsten
Thorsten am 6 Okt. 2016
Bearbeitet: Thorsten am 6 Okt. 2016

0 Stimmen

file_dir = 'C:\Users\mydir';
datestamp=char(datetime('now','TimeZone','local','Format','yyyyMMdd_HHmmss'));
file_name=strcat(file_dir,'sps_',datestamp,'.mat');
>> whos file_name
Name Size Bytes Class Attributes
file_name 1x37 74 char
This works for me.

Tags

Gefragt:

am 6 Okt. 2016

Kommentiert:

am 25 Aug. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by