Reputation: 883
I am having a really hard time understanding the appropriate code/format for creating a datafile in MATLAB. For some reason this particular task just really confuses me.
So I have this script:
function semjudge
SubNum = ('Subject Number: ','s');
files = dir(fullfile('pictures','*.png'));
nFiles = numel(files);
combos = nchoosek(1:nFiles, 2);
index = combos(randperm(size(combos, 1)), :);
picture1 = files(index(1)).name;
picture2 = files(index(2)).name;
image1 = fullfile('pictures',picture1);
image2 = fullfile('pictures',picture2);
subplot(1,2,1); imshow(image1); title(picture1);
subplot(1,2,2); imshow(image2); title(picture2);
uicontrol('Style', 'text',...
'Position', [200 45 200 20],...
'String','How related are these pictures?');
uicontrol('Style', 'text',...
'Position', [50 45 100 20],...
'String','Unrelated');
uicontrol('Style', 'text',...
'Position', [450 45 100 20],...
'String','Closely related');
uicontrol('Style','pushbutton','String','Next Trial',...
'Position', [250 350 100 20],...
'Callback','clf; semjudge()');
h = uicontrol(gcf,...
'Style','slider',...
'Min' ,0,'Max',50, ...
'Position',[100 20 400 20], ...
'Value', 25,...
'SliderStep',[0.02 0.1], ...
'BackgroundColor',[0.8,0.8,0.8]);
set(gcf, 'WindowButtonMotionFcn', @cb);
lastVal = get(h, 'Value');
function cb(s,e)
if get(h, 'Value') ~= lastVal
lastVal = get(h, 'Value');
fprintf('Slider value: %f\n', lastVal);
end
end
end
Pretty simple little script. It pulls two random pictures from a folder, and the user is asked to compare them. All I want is a data file labelled by the Subject Number, something like:
fid = fopen(strcat('data','_',SubNum,'.txt'),'a');
The datafile itself I want to contain the title of each picture, and the values assigned to it by the slider. So, when the user presses the 'Next Trial' button, it saves title(picture1) and title(picture2) as well as lastVal.
I realize this is a very basic question, but the MathWorks documentation on datafiles I find to be very confusing, and I don't understand how to do it.
Upvotes: 1
Views: 374
Reputation: 19870
If I understand your problem correctly it should be something like this (check FPRINTF documentation for details):
fid = fopen(strcat('data','_',SubNum,'.txt'),'a');
fprintf(fid, '%s\t%s\t%f\n', picture1, picture2, lastVal)
fclose(fid);
Based on your code the file name will be ... a little weird. Like 'data_Subject Number: s.txt'
(I hope s
in 2nd line actually will be variable number), but it's up to you to change it.
If you want to print each variable as a single line, you can substitute \t
with \n
.
Upvotes: 3