Your fid exists within the workspace of the function cb. You have to close it inside the function or you should return the fid as an output.
Writing slider output to data file
2 次查看(过去 30 天)
显示 更早的评论
Hi everyone. I have a little MATLAB script that involves a slider uicontrol, and I want the value output of that to be recorded in the datafile. However, I can't seem to get it to work, it keeps giving me error messages that I can't figure out how to fix. Here is my script:
function semjudge
SubNum = input('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)
fid = fopen(strcat('data','_',SubNum,'.txt'),'a');
if get(h, 'Value') ~= lastVal
lastVal = get(h, 'Value');
fprintf(fid, '%s\t%s\t%f\n', picture1, picture2, lastVal);
end
end
fclose(fid);
end
Can anybody help me fix this? I really don't understand why this doesn't work. It just keeps telling me that "fid" isn't defined, or similar error messages.
0 个评论
回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!