How can I save the data from my function as a new .mat file?

조회 수: 1 (최근 30일)
Himangshu
Himangshu 2023년 4월 29일
댓글: Himangshu 2023년 4월 29일
This is my code snippet:
[filepath, name, ext] = fileparts(filename);
newFilename = fullfile(filepath, name , '_RT.mat');
disp(newFilename)
save(newFilename, 'AudioRT', 'VisualRT', 'SOA')
This is the result:
/MATLAB Drive/CHECK/Participant/_RT.mat
Error using save
Cannot create '_RT.mat' because '/MATLAB Drive/CHECK/Participant' does not exist.
Error in CongruentRT (line 42)
save(newFilename, 'AudioRT', 'VisualRT', 'SOA')
But I expect this:
/MATLAB Drive/CHECK/Participant_RT.mat % "/" need not be there
Then, I assume my files would get saved and it won't throw this error.

채택된 답변

Stephen23
Stephen23 2023년 4월 29일
Change
newFilename = fullfile(filepath, name , '_RT.mat');
to
newFilename = fullfile(filepath, [name,'_RT.mat']);
% ^ ^
  댓글 수: 3
Stephen23
Stephen23 2023년 4월 29일
Aaah, you are actually using strings. It helps when you provide this kind of important information.
Try STRCAT:
newFilename = fullfile(filepath, strcat(name,'_RT.mat'))
% ^^^^^^^ ^
Himangshu
Himangshu 2023년 4월 29일
Well, first line of my code snippet shows that. Thanks for your help. I have tested your solution and this works too so I will accept the answer.
Merry answering! :)
I found another solution meanwhile and it seems to work now.
newFilename = sprintf('%s/%s_RT.mat', filepath, name);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by