Well, we are almost done with our first experiment. The last thing we really need to do is save the data for future use.
To do this, we need to do two things. One, we need to ask for a file name at the start of the experiment. Two, we need to save the data at the end. The code for asking for a file name is really easy - we learned it a long time ago.
filename = input('Enter a valid filename: ','s');
Recall that we need the ,'s' at the end to specify we want a string not numeric data.
Now, let us say we have a variable called student_data that we want to save. If we want to save this as a MATLAB variable then it is very easy:
save(filename,'student_data');
The only thing that is a bit quirky here is that need to put our variable name in quotation marks. This will save the variable student_data into a .mat file.
If you want to save the variable student_data as a text file that is easy:
dlmwrite(filename,student_data,'\t');
You will note a few things. Now, with dlmwrite the variable cannot be in quotations. Also, we have added '\t' to specify we want tab delimited text.
MATLAB can also easily save .csv and .xls files in addition a wide range of other formats. I prefer to work with .mat files because it is easier to load.
So, our experiment will now ask for a filename and save the data. I have added one last bit at the end which is some built in analysis so you can how easy it is to incorporate this as well. The code for the final experiment is HERE.
Things To Do
1. You are concerned about writing over an existing file. So, when someone enters a filename you want to check to see if the file exists already. To do this, you could use a bit of code like this. First, make sure you understand what each step is doing. Use the help command as needed. Second, put it in the code and test it to see if it works.
while 1
clc;
subject_number = input('Enter the subject number:\n','s'); % get the subject number
datafile_name = strcat(['ChoiceRT_' subject_number '.mat']);
checker = exist(datafile_name,'file'); % this MATLAB commands checks to see if the file exists
if checker == 0
break;
else
disp('Filename Already Exists!');
WaitSecs(1);
end
end
2. You are worried that the experimental computer will crash. Modify the experiment code so the data file is written each block or trial. This is actually very easy to do, you just have to move the save command.
3. Go back and try all of the Things To Do that you skipped!
WELL DONE for getting this far!
You have worked through the design of a complete experiment.
Move onto the Advanced Tutorial series.
To do this, we need to do two things. One, we need to ask for a file name at the start of the experiment. Two, we need to save the data at the end. The code for asking for a file name is really easy - we learned it a long time ago.
filename = input('Enter a valid filename: ','s');
Recall that we need the ,'s' at the end to specify we want a string not numeric data.
Now, let us say we have a variable called student_data that we want to save. If we want to save this as a MATLAB variable then it is very easy:
save(filename,'student_data');
The only thing that is a bit quirky here is that need to put our variable name in quotation marks. This will save the variable student_data into a .mat file.
If you want to save the variable student_data as a text file that is easy:
dlmwrite(filename,student_data,'\t');
You will note a few things. Now, with dlmwrite the variable cannot be in quotations. Also, we have added '\t' to specify we want tab delimited text.
MATLAB can also easily save .csv and .xls files in addition a wide range of other formats. I prefer to work with .mat files because it is easier to load.
So, our experiment will now ask for a filename and save the data. I have added one last bit at the end which is some built in analysis so you can how easy it is to incorporate this as well. The code for the final experiment is HERE.
Things To Do
1. You are concerned about writing over an existing file. So, when someone enters a filename you want to check to see if the file exists already. To do this, you could use a bit of code like this. First, make sure you understand what each step is doing. Use the help command as needed. Second, put it in the code and test it to see if it works.
while 1
clc;
subject_number = input('Enter the subject number:\n','s'); % get the subject number
datafile_name = strcat(['ChoiceRT_' subject_number '.mat']);
checker = exist(datafile_name,'file'); % this MATLAB commands checks to see if the file exists
if checker == 0
break;
else
disp('Filename Already Exists!');
WaitSecs(1);
end
end
2. You are worried that the experimental computer will crash. Modify the experiment code so the data file is written each block or trial. This is actually very easy to do, you just have to move the save command.
3. Go back and try all of the Things To Do that you skipped!
WELL DONE for getting this far!
You have worked through the design of a complete experiment.
Move onto the Advanced Tutorial series.