GUI control, fwrite, and file control

1 view (last 30 days)
Hi folks,
Pardon my naivete, but I'm having trouble with a GUI. I want to adjust a slider bar, and then fwrite the new value of the slider bar to a file. First, what works.
I can get a file identifier in a function, and pass that identifier to another function to do the actual writing, and then return control to the first file.
function[] = filecreate()
a=fopen('b.txt','at');
filewrite(a);
fclose(a);
end
function[] = filewrite(b)
fwrite(b, 'HI');
end
So, as proof of concept, I should be able to pass the file identifier between functions. Now, onto the sliderbar GUI. I can write to a file if I open, write to, and close a file all within the slider bar callback function.
However, if I open a file outside of the callback and pass the file identifier to the callback, then I can't seem to write to the file.
I get the following error:
??? Error using ==> fwrite -Invalid file identifier. Use fopen to generate a valid file identifier. Error in ==> practice_slider>ed_call at 32 -fwrite(Speed_temp.file, 'H'); ??? Error while evaluating uicontrol Callback
Here is the function calling the GUI
function[] = slidingbarcontrol()
fid=fopen('a.txt','at');
practice_slider(fid);
fclose(fid);
And, here is the GUI.
function[] = practice_slider(file_id)
Speed.file=file_id;
fwrite(Speed.file,'Hello'); * %This successfully writes to the file*
Speed.last=uint8(0);
Speed.fh = figure('units','pixels',...
'position',[300 300 300 100],...
'menubar','none',...
'name','Speed',...
'numbertitle','off',...
'resize','off');
Speed.sl = uicontrol('style','slide',...
'unit','pix',...
'position',[20 10 260 30],...
'min',0,'max',100,'val',0);
Speed.ed = uicontrol('style','text',...
'unit','pix',...
'position',[20 50 260 30],...
'fontsize',16,...
'string','0');
set([Speed.ed,Speed.sl],'call',{@ed_call,Speed});
function [] = ed_call(varargin) %slider callback
[h,Speed_temp] = varargin{[1,3]};
fwrite(Speed_temp.file, 'H'); *%This fwrite causes error.*
switch h
Does anyone have any thoughts??? Thanks much. Tom

Accepted Answer

Sean de Wolski
Sean de Wolski on 18 Apr 2012
The error is occuring because you don't have a uiwait/uiresume in your GUI to pause execution. Thus the file opens, the gui opens, the file closes, you slide and get the error. To fix it, pause execution by intserting a uiwait() at the end of the GUI and a uiresume when you close it.
doc uiwait
doc uiresume
doc waitfor %another approach

More Answers (1)

T Fab
T Fab on 19 Apr 2012
Sean, thanks very much. Your comments made me go back and re-examine just what was going on with my program. I deleted the command to close the output file in the function slidingbarcontrol(), and added a pushbutton to the practice_slider GUI that, when clicked, will fclose the file, and delete the GUI. I put the corrected (and newly functioning) version here so if someone needs to see the implemented final answer, they have it available. Thanks again. Tom
function[] = slidingbarcontrol()
fid=fopen('a.txt','at');
practice_slider(fid);
%fclose(fid); %This line is now deleted.
end
function[] = practice_slider(file_id)
Speed.file=file_id;
Speed.last=uint8(0);
Speed.fh = figure('units','pixels',...
'position',[300 300 300 100],...
'menubar','none',...
'name','Speed',...
'numbertitle','off',...
'resize','off');
Speed.pb = uicontrol('style','push',... %I added this pushbutton
'unit', 'pix',... %to close the GUI and
'position', [225 70 75 30],...%also close the output file.
'string', 'Push to Close');
Speed.sl = uicontrol('style','slide',...
'unit','pix',...
'position',[20 10 260 30],...
'min',0,'max',100,'val',0);
Speed.ed = uicontrol('style','text',...
'unit','pix',...
'position',[110 50 80 30],...
'fontsize',16,...
'string','0');
set([Speed.pb, Speed.ed,Speed.sl],'call',{@ed_call,Speed});
function [] = ed_call(varargin) %slider callback
[h,Speed_temp] = varargin{[1,3]};
fwrite(Speed_temp.file, 'H'); %This works now.
switch h
case Speed_temp.ed
L = get(Speed_temp.sl,{'min','max','value'});
E = uint8(str2double(get(h,'string')));
if E >= L{1} && E <= L{2}
set(Speed_temp.sl,'value',E)
else
set(h,'string',L{3})
end
case Speed_temp.sl
new_speed=uint8(get(h,'value'));
set(Speed_temp.ed,'string',new_speed)
if(new_speed>Speed_temp.last)
fwrite(file_id, 'F'); %Now I can fwrite here.
elseif(Speed_temp.last>new_speed)
fwrite(file_id, 'S'); %Now I can fwrite here.
else
end
fwrite(file_id, 'DONE'); %Now I can fwrite here.
Speed_temp.last=new_speed;
set([Speed_temp.ed,Speed_temp.sl],'call',{@ed_call,Speed_temp});
case Speed_temp.pb
fclose(Speed_temp.file); %This will now close the output file.
close(gcbf); %This will close the GUI.
otherwise
end

Community Treasure Hunt

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

Start Hunting!