Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Trouble With GUI Button

조회 수: 1 (최근 30일)
Steve
Steve 2013년 3월 31일
마감: MATLAB Answer Bot 2021년 8월 20일
I'm trying to set up a function that I can use to take a snapshot of something I work on before and after by pushing a button. Later I'm going to add something to append them together. My problem seems to be that I can only execute the print command once. I think it might be because it won't save over the new .ps file that I'm creating. I've tried the filename command to rename the files, but it doesn't seem to work. Also, I am having troubles changing the string of the button each time it is pushed. This is the section of code I'm having troubles with:
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
count = 0
pushed = get(hObject, 'Value');
if count = 0
filename = 'Before.ps';
print('-dps','-s',gcs, filename);
count = 1;
set(hObject, 'String', 'After') ;
else
filename = 'After.ps';
print('-dps','-s',gcs, filename);
count = 0;
set(hObject, 'String', 'Before') ;
end

답변 (2개)

Image Analyst
Image Analyst 2013년 3월 31일
Make count global or persistent.

Steve
Steve 2013년 4월 1일
편집: Image Analyst 2013년 4월 1일
This is the error I receive when I try to run it:
"??? Error using ==> print
Error using ==> graphics\private\inputcheck
Multiple inputs that look like filenames: 'simulink_file_1' and 'Before.ps'
Error in ==> BeforeAfter>pushbutton1_Callback at 87
print('-dps','-s',gcs,filename);
Error in ==> gui_mainfcn at 75
feval(varargin{:});
Error in ==> BeforeAfter at 42
gui_mainfcn(gui_State, varargin{:});
??? Error while evaluating uicontrol Callback."
Clearly there is a problem with the use of the filename. I'm trying to print the current screen as a .ps file and save it under a predefined name. Unfortunately, it seems to be reading it as a command to print both the current screen AND the file yet to be printed. Any suggestions? Also, the count variable does not appear in my workspace when I execute without the file-naming. . .
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global count;
count=0;
pushed = get(hObject, 'Value');
for count = 0
filename = 'Before.ps';
print('-dps','-s',gcs,filename);
count = 1;
set(hObject, 'String', 'After') ;
end
for count = 1
filename = 'After.ps';
print('-dps','-s',gcs,filename);
count = 0;
set(hObject, 'String', 'Before') ;
end

Community Treasure Hunt

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

Start Hunting!

Translated by