Matlab 查看轴上显示的图像

Matlab 查看轴上显示的图像,matlab,matlab-figure,matlab-guide,Matlab,Matlab Figure,Matlab Guide,() 上面的图像是我的最终输出,open按钮用于查看各个轴上显示的图像。 我使用了以下代码来显示 function open1_Callback(filename, hObject, eventdata, handles) % hObject handle to open1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with ha

()

上面的图像是我的最终输出,
open
按钮用于查看各个轴上显示的图像。 我使用了以下代码来显示

function open1_Callback(filename, hObject, eventdata, handles)
% hObject    handle to open1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
fid=fopen(filename);
ax = handles.(sprintf('axes%d', 6));
imshow(ax)
其中,6是轴数。但我得到了这样的错误
未定义的变量句柄
要在轴上显示图像,我使用了以下代码

function displayResults(filename,hObject, eventdata, handles)

% Open 'filename' file... for reading...
fid = fopen(filename);
for N=6:1:10
    imagename = fgetl(fid);
    if ~ischar(imagename), break, end       % Meaning: End of File...
    [x,map]=imread(imagename);
    rgb=ind2rgb(x,map);
    ax = handles.(sprintf('axes%d', N));
    image(rgb, 'Parent', ax);
    set(ax, 'Visible','off');
    %xlabel(imagename);  
end
guidata(hObject,handles)
文件名是一个文本文件。
如何使用按钮显示相应的图像?

这是因为您弄乱了指南生成的代码。 通常回调函数定义如下所示:

function SomeButton_Callback(hObject, eventdata, handles)
但是在你的代码中你写

function open1_Callback(filename, hObject, eventdata, handles)
但guide仍按此特定顺序向回调函数发送三个参数(
hObject
eventdata
、和
句柄
)。因此,MatLab变得混乱,并抛出一个错误

您最好将文件名放入
*\u OpeningFcn
函数中的
句柄
结构中,然后在所有calback中使用它

*\u OpeningFcn
的末尾,您应该添加以下内容:

% Here you may put all the data you need in your GUI
% just be sure to keep all the fields in handles structure from overwriting
% Safe way is to add MyData field and add all the stuff to it
handles.MyData.ListFileName = 'FileName.txt';
% the next two lienes are generated by GUIDE

% Update handles structure
guidata(hObject, handles);
% Here you may put all the data you need in your GUI
% just be sure to keep all the fields in handles structure from overwriting
% Safe way is to add MyData field and add all the stuff to it
handles.MyData.ListFileName = 'FileName.txt';
handles.MyData.FileNames = {}; % here we store all the image names
handles.MyData.Images    = {}; % here we store all images
% Now we parse data from the file
fid=fopen(handles.MyData.ListFileName);
for N=6:1:10
    imagename = fgetl(fid);
    if ~ischar(imagename), break, end       % Meaning: End of File...
    [x,map]=imread(imagename);
    rgb=ind2rgb(x,map);
    ax = handles.(sprintf('axes%d', N));
    image(rgb, 'Parent', ax);
    set(ax, 'Visible','off');
    %xlabel(imagename);

    % store file name and image itself for later use
    handles.MyData.Images{N} = rgb;
    handles.MyData.FileNames{N} = imagename;

    % we have buttons like open1 open2 open3 etc...
    % add some info to the open buttons
    % so they will be aware which image they display
    btn = handles.(sprintf('open%d', N));
    set(btn, 'UserData',N);
end
% dont' forget to close the file
fclose(fid);
% the next two lienes are generated by GUIDE

% Update handles structure
guidata(hObject, handles);
然后在你的按钮的回调函数中

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

% open the file
fid=fopen(handles.MyData.ListFileName);
% load lines from the file
% and do what is needed
for N=6:1:10
    imagename = fgetl(fid);
    if ~ischar(imagename), break, end       % Meaning: End of File...
    [x,map]=imread(imagename);
    rgb=ind2rgb(x,map);
    ax = handles.(sprintf('axes%d', N));
    image(rgb, 'Parent', ax);
    set(ax, 'Visible','off');
    %xlabel(imagename);  
end
% dont' forget to close the file
fclose(fid);
% If your callback function modifies data in handles.MyData structure
% you MUST update it back otherwise subsequent call-backs will not see it
guidata(hObject, handles);
是的,使用
fopen
功能打开的所有文件都应使用
fclose
关闭。当您无法在您喜爱的编辑器中更新您的文件,因为其他程序正在使用它时,您将很难了解这一点

另见()

更新以反映评论中的讨论:

要实现您想要的行为,我将执行以下操作:

% Here you may put all the data you need in your GUI
% just be sure to keep all the fields in handles structure from overwriting
% Safe way is to add MyData field and add all the stuff to it
handles.MyData.ListFileName = 'FileName.txt';
% the next two lienes are generated by GUIDE

% Update handles structure
guidata(hObject, handles);
% Here you may put all the data you need in your GUI
% just be sure to keep all the fields in handles structure from overwriting
% Safe way is to add MyData field and add all the stuff to it
handles.MyData.ListFileName = 'FileName.txt';
handles.MyData.FileNames = {}; % here we store all the image names
handles.MyData.Images    = {}; % here we store all images
% Now we parse data from the file
fid=fopen(handles.MyData.ListFileName);
for N=6:1:10
    imagename = fgetl(fid);
    if ~ischar(imagename), break, end       % Meaning: End of File...
    [x,map]=imread(imagename);
    rgb=ind2rgb(x,map);
    ax = handles.(sprintf('axes%d', N));
    image(rgb, 'Parent', ax);
    set(ax, 'Visible','off');
    %xlabel(imagename);

    % store file name and image itself for later use
    handles.MyData.Images{N} = rgb;
    handles.MyData.FileNames{N} = imagename;

    % we have buttons like open1 open2 open3 etc...
    % add some info to the open buttons
    % so they will be aware which image they display
    btn = handles.(sprintf('open%d', N));
    set(btn, 'UserData',N);
end
% dont' forget to close the file
fclose(fid);
% the next two lienes are generated by GUIDE

% Update handles structure
guidata(hObject, handles);
*\u OpeningFcn
的末尾添加以下内容:

% Here you may put all the data you need in your GUI
% just be sure to keep all the fields in handles structure from overwriting
% Safe way is to add MyData field and add all the stuff to it
handles.MyData.ListFileName = 'FileName.txt';
% the next two lienes are generated by GUIDE

% Update handles structure
guidata(hObject, handles);
% Here you may put all the data you need in your GUI
% just be sure to keep all the fields in handles structure from overwriting
% Safe way is to add MyData field and add all the stuff to it
handles.MyData.ListFileName = 'FileName.txt';
handles.MyData.FileNames = {}; % here we store all the image names
handles.MyData.Images    = {}; % here we store all images
% Now we parse data from the file
fid=fopen(handles.MyData.ListFileName);
for N=6:1:10
    imagename = fgetl(fid);
    if ~ischar(imagename), break, end       % Meaning: End of File...
    [x,map]=imread(imagename);
    rgb=ind2rgb(x,map);
    ax = handles.(sprintf('axes%d', N));
    image(rgb, 'Parent', ax);
    set(ax, 'Visible','off');
    %xlabel(imagename);

    % store file name and image itself for later use
    handles.MyData.Images{N} = rgb;
    handles.MyData.FileNames{N} = imagename;

    % we have buttons like open1 open2 open3 etc...
    % add some info to the open buttons
    % so they will be aware which image they display
    btn = handles.(sprintf('open%d', N));
    set(btn, 'UserData',N);
end
% dont' forget to close the file
fclose(fid);
% the next two lienes are generated by GUIDE

% Update handles structure
guidata(hObject, handles);
然后在“打开”按钮的回调函数中执行以下操作

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

N   = get(hObject,'UserData');
rgb = handles.MyData.Images{N};
ax  = handles.(sprintf('axes%d', N));
% create figure in 'modal' mode, so user have to close it to continue
figure('WindowStyle','modal', 'Name',handles.MyData.FileNames{N} );
% show image
image(rgb, 'Parent', ax);
set(ax, 'Visible','off');
% If your callback function modifies data in handles.MyData structure
% you MUST update it back otherwise subsequent call-backs will not see it
guidata(hObject, handles);

基本上,这个回调是通用的:它应该在不修改所有打开按钮的情况下工作。您甚至可以更改
open2
open3
的回调函数。。。
open1\u回调指南中的按钮

我只想打开显示在轴上的图像,它就在
open
按钮上方。为了使查看器对大尺寸图像感到满意,您有多个
/
打开
按钮对:
显示图像,
打开
按钮应以更大的尺寸显示相应的图像。是否正确?这意味着您没有
open6
按钮您确定数据文件中列出了足够的图像吗?这意味着N大于handles.MyData.ImagesN中的元素数为6到10,即5。然后根据上面的代码,
处理.MyData.Images
也会得到5幅图像。那么代码的哪一部分是错误的?请删除您之前的评论,以便进一步交谈