Saving .mat file with different names

조회 수: 38 (최근 30일)
TANMAYEE PATHRE
TANMAYEE PATHRE 2019년 2월 22일
답변: rahul PAL 2022년 11월 30일
I am doing a test study in which I test the participants for three conditions. Each participant is given an ID (1,2,...etc.). The response of the participants is saved in form or .mat file. I would like to save the response such that NH_ID_Condition. For example, If I am testing for 1st participant and 2nd condition it should save as NH_1_2. How do I use any of the loops so that I dont have to rename the variable after the experiment is finished.

답변 (2개)

Bjorn Gustavsson
Bjorn Gustavsson 2019년 2월 22일
You can use the functional form of save:
X = data_of_interest1;
Y = data_of_interest2; % and
save_name = sprintf('NH_%03d_%03d.mat',ID,Condition)
save(save_name,'X','Y')
That way you'll create one file with the X and Y data each ID-Condition combination. I strongly suggest that you use
%03d instead of %d when generating your save_name variable. The former will generate names with prepaded zeros like:
NH_001_012.mat - that way it becomes that much easier to list and handle the files in order. Depending on the number of participants and conditions you can change from %03 to whatever suits your needs.
HTH

rahul PAL
rahul PAL 2022년 11월 30일
for k = 1 : 12
a = rand;
b = rand;
filename = sprintf('datset_%06d.mat',k)
save(filename,'a','b')
end

카테고리

Help CenterFile Exchange에서 Testing Frameworks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by