Creating file names for save command

조회 수: 9 (최근 30일)
Paul Nel
Paul Nel 2016년 10월 6일
댓글: Nagabhushan SN 2018년 8월 25일
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
Paul Nel
Paul Nel 2016년 10월 6일
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');
Nagabhushan SN
Nagabhushan SN 2018년 8월 25일
Same error:
save('temp.mat', data);
Error using save
Must be a string scalar or character vector.

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

답변 (2개)

KSSV
KSSV 2016년 10월 6일
편집: KSSV 2016년 10월 6일
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
KSSV
KSSV 2016년 10월 6일
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 2016년 10월 6일
Prefer fullfile to join path and file names.

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


Thorsten
Thorsten 2016년 10월 6일
편집: Thorsten 2016년 10월 6일
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.

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by