在MATLAB GUIDE gui中调用函数

时间:2016-12-31 18:51:55

标签: image matlab user-interface matlab-guide

我有一个名为'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

selectImage:

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

现在我必须将这些功能集成到我的gui中。我想做的是用“让我们开始”之类的按钮调用'main.m'脚本一次,然后显示免责声明。 然后重复该过程仅调用“下一步”按钮,该按钮调用“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

-selectFolder:

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)

1 个答案:

答案 0 :(得分:0)

在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;

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

关于GUI的架构,有很多可能性。

评论:看看你在问题底部发布的两个回调,看来你的GUI至少有一个axes,但是,你创建了两个他们是一个新的figure因此不清楚axes

的作用

现在考虑你的问题

  

我想要做的就是打电话给#main; m'脚本只需一次按下按钮,例如“让我们开始”#39;并将显示免责声明。然后重复这个过程只调用' Next'按钮,调用" selectFolder.m'并按照上述步骤显示图像

  • 致电' main.m'脚本只需一次按下按钮,例如“让我们开始”#39;并将显示免责声明

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

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

  • 然后重复这个过程只调用' Next'按钮,调用" selectFolder.m'并按照上述步骤显示图像

要执行此操作,您必须从主广告中删除对selectFolder的通话,并在Next按键回拨中移动您的正文。

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

希望这有帮助。

Qapla'