在 MATLAB GUIDE gui 中调用函数

Call a function in MATLAB GUIDE gui

我有一个名为 'main.m' 的脚本,它基本上采用我保存所有图像并将它们插入数组的路径。它将图像名称保存在 .dat 文件中并调用名为 'selectFolder.m'.

的函数

下面的脚本和函数我都贴出来了,我的要求在最下面

%% Folders
imgFolder   = './1.Dataset/';
functFolder = './2.Functions/' ;
%resFolder   = './3.Results/';

%% Add path
addpath(genpath(imgFolder));
addpath(genpath(functFolder));

%% Listing Folders where my images are at

myFolder1 = '../Always'; %folder path
[..] %12 folders in total
myFolder12 = '../Random'; %folder path
%% Distinguish folder 'Always' & 'Random'

% Always Folders: subset of images for all users
mfA = {myFolder1, myFolder3, myFolder5, myFolder7, myFolder9, myFolder11};
dimA = length(mfA);
% Random Folders: subset of images randomly showed
mfR = {myFolder2, myFolder4, myFolder6, myFolder8, myFolder10, myFolder12}; 
dimR = length(mfR);
% check if folders are present
for i = 1:dimA
    if ~isdir(mfA{i})
        errorMessage = sprintf('Error: The following folder does not exist:\n%s', mfA{i});
        uiwait(warndlg(errorMessage)); 
        return;
    end
end
for j = 1:dimR
    if ~isdir(mfR{j})
        errorMessage = sprintf('Error: The following folder does not exist:\n%s', mfR{j});
        uiwait(warndlg(errorMessage)); 
        return;
    end
end
%% Take images and insert'em in Arrays

% Always
MyImgs1 = dir(fullfile(mfA{1}, '*.jpg'));
[..] %for every cell 
MyImgs6 = dir(fullfile(mfA{6}, '*.jpg'));

% Random
MyImgs1r = dir(fullfile(mfR{1}, '*.jpg'));
[..] %for every cell 
MyImgs6r = dir(fullfile(mfR{6}, '*.jpg'));

% create arrays with images names
Array_mfA = {MyImgs1.name, MyImgs2.name, MyImgs3.name, MyImgs4.name, MyImgs5.name, MyImgs6.name};
Array_mfR = {MyImgs1r.name, MyImgs2r.name, MyImgs3r.name, MyImgs4r.name, MyImgs5r.name, MyImgs6r.name};

%% Print content of array on file

fileIDA = fopen('2.Functions/Array_Always.dat','w');
formatSpec = '%s,';
nrows = length(Array_mfA);
for row = 1 : nrows
    fprintf(fileIDA, formatSpec, Array_mfA{row});
end
fclose(fileIDA);

fileIDR = fopen('2.Functions/Array_Random.dat','w');
formatSpec = '%s,';
nrows = length(Array_mfR);
for row = 1 : nrows
    fprintf(fileIDR, formatSpec, Array_mfR{row});
end
fclose(fileIDR);

%disclaimer
nrc = 1;
file = fopen('2.Functions/disclaimer.dat', 'w');
fprintf(file, '%d', nrc);
fclose(file);

%% call function

selectFolder(mfA, mfR);

这个函数需要两个数组作为输入,这些数组包含我排序的所有图像的名称。它执行一些操作,然后调用另一个函数 'selectImage.m' 全屏显示所选图像。

function [] = selectFolder(mfA, mfR)
    clc

    %% Open Arrays from file

    % Always
    fileID = fopen('2.Functions/Array_Always.dat', 'rt');
    Array_A = textscan(fileID,'%s', 'Delimiter', ',');
    fclose(fileID);

    % Random
    fileID2 = fopen('2.Functions/Array_Random.dat', 'rt');
    Array_R = textscan(fileID2,'%s', 'Delimiter', ',');
    fclose(fileID2);

    %% Show Disclaimer
    file = fopen('2.Functions/disclaimer.dat', 'r');
    dis = fscanf(file, '%d');
    fclose(file);
    if (dis == 1)
        set(gcf,'Toolbar','none','Menubar','none', 'NumberTitle','off');
        set(gcf,'units','normalized','outerposition',[0 0 1 1]);
        hAx  = gca;
        set(hAx,'Unit','normalized','Position',[0 0 1 1]);
        imshow('1.Dataset/Disclaimer/DIS.jpg');
        drawnow;

        nrc = 0;
        file = fopen('2.Functions/disclaimer.dat', 'w');
        fprintf(file, '%d', nrc);
        fclose(file);

        return;
    end
    %% select random folder from 'Array_A' aka Always Array

    dimA = length(mfA);

    if ~isempty(Array_A{1})
        rndn = randperm(dimA, 1);
        A_check = Array_A;
        while isequal(A_check,Array_A)
            Array_A = selectImage(mfA{rndn}, Array_A);
            if isequal(A_check,Array_A)
                rndn = randperm(dimA, 1);
            end
        end
        fileIDA = fopen('2.Functions/Array_Always.dat','w');
        formatSpec = '%s,';
        nrows = cellfun('length', Array_A);
        for row = 1 : nrows
            fprintf(fileIDA, formatSpec, Array_A{1}{row});
        end
        fclose(fileIDA);
        return;
    end

    %%  select random folder from 'Array_R' aka Random Array

    if ~isempty(Array_R{1})
        dimR = length(mfR);
        rndnr = randperm(dimR, 1);
        R_check = Array_R;
        while isequal(R_check,Array_R)
            Array_R = selectImage(mfR{rndnr}, Array_R);
            if isequal(R_check, Array_R)
                rndnr = randperm(dimR, 1);
            end
        end
        fileIDR = fopen('2.Functions/Array_Random.dat','w');
        formatSpec = '%s,';
        nrows = cellfun('length', Array_R);
        for row = 1 : nrows
            fprintf(fileIDR, formatSpec, Array_R{1}{row});
        end
        fclose(fileIDR);
    end
end

选择图片:

function [ Array ] = selectImage( myFolder, Array )
%% Check

MyImgs = dir(fullfile(myFolder, '*.jpg'));
dim = length(MyImgs);
n = 0;
for i = 1 : dim 
    MyImgs(i).name
        if ~any(strcmp(Array{1}, MyImgs(i).name))
            disp(MyImgs(i).name);disp('not present in ');disp(myFolder);
            n = n + 1;
        end
end
if (n == dim)
   disp('empty folder')
   return;
end

rN = randperm(dim, 1);
baseFileName = MyImgs(rN).name;

while ~any(strcmp(Array{1}, baseFileName))
     fprintf(1, 'not present %s\n', baseFileName);
     rN = randperm(dim, 1);
     baseFileName = MyImgs(rN).name;
end

%% Dispay image

dim = cellfun('length', Array);

for i = 1 : dim
    if strcmp(baseFileName, Array{1}(i))
         Array{1}(i) = [];
         break
    end
end

fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);

set(gcf,'Toolbar','none','Menubar','none', 'NumberTitle','off');
set(gcf,'units','normalized','outerposition',[0 0 1 1]);
hAx  = gca;
set(hAx,'Unit','normalized','Position',[0 0 1 1]);
imshow(imageArray);  % Display image.
drawnow;

end

现在我必须将这些功能集成到我的图形用户界面中。我想要做的是只调用 'main.m' 脚本一次,并使用“让我们开始”之类的按钮,然后将显示免责声明。 然后重复仅调用 'Next' 按钮的过程,该按钮调用 'selectFolder.m' 并使用上述过程显示图像。

这样可以吗?我的意思是,如何将变量 'mfA' 和 'mfR' 传递给 selectFolder?有更好更简单的方法吗?

gui 中的代码如下:

-主要:

% --- Executes on button press in Start.
function Start_Callback(hObject, eventdata, handles)
% hObject    handle to Start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


axes(handles.axes1);
figure
main

-选择文件夹:

function Next_Callback(hObject, eventdata, handles)
% hObject    handle to Next (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

axes(handles.axes1);
figure %show the image in another window
selectFolder(mfA, mfR)

在 GUI 的回调之间共享变量的一种简单方法是使用 guidata 函数。

关于您的特定变量 mfAmfR 您可以使用 guidata 以这种方式存储它们:在您生成要与其他回调共享的变量的回调中,您可以插入以下代码:

% Get the GUI data
my_guidata=guidata(gcf);
%
% section of your code in which you create the mfA and mfR vars
%
% Store the variables to be shared among the callbacks in the guidata
my_guidata.mfA=mfA;
my_guidata.mfR=mfR;
% Save the updated GUI data
guidata(gcf,my_guidata);

在你想要检索数据的回调中,你可以插入 以下代码:

% Get the GUI data
my_guidata=guidata(gcf);
% Retrieve the data from the GUI data structure
mfA=my_guidata.mfA;
mfR=my_guidata.mfR;

在这两个示例中,结构 my_guidata 包含 GUI 的句柄和您定义的其他变量。

关于 GUI 的体系结构,有很多可能性。

首先发表评论:查看您在问题底部发布的两个回调,似乎您的 GUI 至少有一个 axes,但是,您在两个回调中都创建了他们是一个新的 figure 所以不清楚 axes

的作用

现在考虑您的问题

What I want to do is call the 'main.m' script just one time with a button like 'Let's Start' and with that will show the disclaimer. Then repeat the process calling only the 'Next' button, which calls 'selectFolder.m' and display the images with the procedure described above

  • 只需调用一次 'main.m' 脚本,使用“让我们开始”之类的按钮即可显示免责声明

您只需在 Start 按钮回调中复制 main 的相关代码。

请注意,显示免责声明的代码实际上在您的 selectFolder 函数中,因此您必须将其移动到 Start回调。

  • 然后重复仅调用 'Next' 按钮的过程,后者调用 'selectFolder.m' 并使用上述过程显示图像

为此,您必须从主函数中删除对 selectFolder 的调用,并在 Next 按钮回调中移动您的正文。

您也可以在 GUI .m 文件中复制 selectImage

希望对您有所帮助。

Qapla'