GUI control, fwrite, and file control
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
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
0 Comments
More Answers (1)
See Also
Categories
Find more on String Parsing 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!