Saving .mat file with different names

33 views (last 30 days)
TANMAYEE PATHRE
TANMAYEE PATHRE on 22 Feb 2019
Answered: rahul PAL on 30 Nov 2022
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.

Answers (2)

Bjorn Gustavsson
Bjorn Gustavsson on 22 Feb 2019
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 on 30 Nov 2022
for k = 1 : 12
a = rand;
b = rand;
filename = sprintf('datset_%06d.mat',k)
save(filename,'a','b')
end

Categories

Find more on Testing Frameworks in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!