Creating windows using GUI vs programmatically

3 次查看(过去 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
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 CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by