简体   繁体   English

在MATLAB GUIDE gui中调用函数

[英]Call a function in MATLAB GUIDE gui

I have a script called 'main.m' that basically takes the paths where I've saved all my images and insert them in arrays. 我有一个名为“ main.m”的脚本,该脚本基本上采用保存所有图像并将它们插入数组的路径。 It saves the images name in a .dat file and call a function named 'selectFolder.m'. 它将图像名称保存在.dat文件中,然后调用名为“ selectFolder.m”的函数。

I posted all the script and functions under, my request is at the bottom. 我在下面发布了所有脚本和函数,我的要求在底部。

%% 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);

This function takes two array as input, these array contains all the names of my images sorted. 该函数将两个数组作为输入,这些数组包含排序后的所有图像名称。 It does some operation and then it calls another function 'selectImage.m' that displays fullscreen the selected image. 它执行一些操作,然后调用另一个函数“ 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: 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

Now I have to integrate these functions in my gui. 现在,我必须在我的GUI中集成这些功能。 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. 我想做的是仅用一次“ Let's Start”之类的按钮调用“ main.m”脚本,并显示免责声明。 Then repeat the process calling only the 'Next' button, which calls 'selectFolder.m' and display the images with the procedure described above. 然后重复仅调用“下一步”按钮的过程,该按钮将调用“ selectFolder.m”并按照上述步骤显示图像。

Is it possibile to do it this way? 这样可以吗? I mean, how can I pass the variable 'mfA' and 'mfR' to selectFolder? 我的意思是,如何将变量'mfA'和'mfR'传递给selectFolder? Is there a better and simpler way to do it? 有没有更好,更简单的方法呢?

The code in the gui is like: gui中的代码如下:

-main: -主要:

% --- 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)

An easy way to share variables among the callback of a GUI is to use the guidata function. 在GUI的回调之间共享变量的一种简单方法是使用guidata函数。

With respect to your specific variables mfA and mfR you can use guidata to store them, this way: in the callback in which you generate the variables you want to share with other callback you can insert the following code: 对于特定的变量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);

In the callback in which you wnat to retreive the data, you can insert the following code: 在您拥有检索数据的回调中,可以插入以下代码:

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

In both the examples, the struct my_guidata holds the handles of the GUI and the additional varaibles you have defined. 在两个示例中,结构my_guidata包含GUI的句柄以及您定义的其他变量。

With respect to the the architecture of the GUI, there are lots of possibilities. 关于GUI的体系结构,有很多可能性。

Firt a comment: looking at the two callback you've posted at the bottom of your question, it seems that your GUI has, at least, one axes , nevertheless, you create, in both of them a new figure so it is not clear the role of that axes 提出一个评论:查看问题底部的两个回调,看来您的GUI至少有一个axes ,但是您在两个axes中都创建了一个新figure因此不清楚axes的作用

Considering now your questions 现在考虑您的问题

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. 我想做的是仅用一次“ Let's Start”之类的按钮调用“ main.m”脚本,并显示免责声明。 Then repeat the process calling only the 'Next' button, which calls 'selectFolder.m' and display the images with the procedure described above 然后重复仅调用“下一步”按钮的过程,该按钮调用“ selectFolder.m”,并按照上述步骤显示图像

  • call the 'main.m' script just one time with a button like 'Let's Start' and with that will show the disclaimer 只需使用“让我们开始”之类的按钮一次调用“ main.m”脚本,它将显示免责声明

You have just to copy the relevant code of your main in the Start pushbutton callback. 您只需要在“ Start按钮回调中复制main代码的相关代码即可。

Notice that the code which shows the disclaimer is actually in your selectFolder function, so you have to move it in the Start callback. 注意,显示免责声明的代码实际上在selectFolder函数中,因此您必须在Start回调中将其移动。

  • Then repeat the process calling only the 'Next' button, which calls 'selectFolder.m' and display the images with the procedure described above 然后重复仅调用“下一步”按钮的过程,该按钮调用“ selectFolder.m”,并按照上述步骤显示图像

to do this, you have to remove the call to selectFolder from the main and move the body of your in the Next pushbotton callback. 为此,您必须从主调用中删除对selectFolder的调用, selectFolderNext pushbotton回调中移动您的主体。

Also you can copy the selectImage in the GUI .m file. 您也可以在GUI .m文件中复制selectImage

Hope this helps. 希望这可以帮助。

Qapla' Qapla”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM