Creating windows using GUI vs programmatically
조회 수: 4 (최근 30일)
이전 댓글 표시
I am using GUIDE to create a window with several components (push buttons, axes, slider). I have the window built and need to add the callbacks to the window code in the editor. After reading more about callbacks, it appears that creating a window with GUIDE requires different callbacks than creating it purely programatically. I have a code which works in the way I need but I am attempting to use this code as the callback of a push button so that when the button is pressed, the code executes.
The code:
myFolder = uigetdir('C:\Users\c13459232\Documents\MATLAB'); % Generate command window to choose a folder
if ~isdir(myFolder) % if the directory is not a valid path
errorMessage = sprintf('Error: the following folder does not exist: \n%s', myFolder); % print this error message
uiwait(warndlg(errorMessage)); % block the execution of program and wait to resume
return;
end
outFolder = fullfile(myFolder, 'output'); % build full file name from parts in folder 'Output'
mkdir(outFolder); % create folder
filePattern = fullfile(myFolder, '*.asc'); % Call all files with '.asc' from the chosen folder
Files = dir(filePattern); % list folder contents
finishCell = cell(length(Files));
for k = 1 : length(Files) % for all files files in the folder
baseFileName = Files(k).name;
FileName = fullfile(myFolder, baseFileName);
fid = fopen(FileName); % open the file from chosen folder
Cell = textscan( fid, '%d', 'delimiter', ';'); % scanning data from files
fclose(fid); % close file from chosen folder
Data = cell2mat(Cell); % convert the cell data to matrix
N = 1024; % Number of numbers per row
Finish0 = reshape(Data, N, [])'; % reshape the data into the correct format
finishCell{k} = Finish0;
newFileName = fullfile(outFolder, ['finish_' sprintf('%03d', k) '.txt']); % finish_001.asc, finish_002.asc, ...
dlmwrite(newFileName, Finish0, ';'); % create new files
end
The GUIDE generated code where the callback will go:
% --- 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)
The first if statement and uigetdir can be removed and placed in the callback for an open tool in the toolbar. How do I adapt my code to fit as a callback? Eventdata is not often used and in examples I've seen online, the code is simply added after the hobject, handles and eventdata comments.
댓글 수: 1
Adam
2017년 3월 21일
Whether your code is done in a programmatic callback or a GUIDE one you have to be able to share data around the GUI.
Then there is no reason why your callback code can't go in either a GUIDE callback or a programmatic one, it amounts to the same thing. In a GUIDE GUI your data can be stored on the 'handles' struct for sharing, in a programmatic GUI you can create your own struct or create the GUI in a class which is what I always do because it is most intuitive.
The only difficulty though is getting access to all the things you need in the callback, which is what the link I included shows.
답변 (0개)
참고 항목
카테고리
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!