Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

displaying images on axes in MATLAB

Asked by Chethan on 27 Apr 2013

I have a GUI with 5 axes in it, where images which are stored in text file(notepad) are displayed. Images in text file are not static, it keeps updating with new ones. I mean for first search images are different and after closing all windows again if i run same program for next search different images may get saved in notepad.

    function displayResults(filename, header)
    figure('Position',[200 100 700 400], 'MenuBar', 'none', 'Name', header, 'Resize',     'off', 'NumberTitle', 'off');
    % Open 'filename' file... for reading...
    fid = fopen(filename);
    for N=1:5
        imagename = fgetl(fid);
        if ~ischar(imagename), break, end       % Meaning: End of File...
         (x) = imread(imagename);
         axes(handles.axesN);
         imshow(fname);
         xlabel(imagename);
    end
    fclose(fid);

filename is text file

I need to fit these images on all 5 axes, but I'm getting error like undefined variable handles.axesN How can i go for it?

1 Comment

Jan Simon on 29 Apr 2013

The question is not clear to me. You have posted the code 3 times now, but this does not increase the chance to understand it.

Chethan

Tags

Products

No products are associated with this question.

2 Answers

Answer by Jan Simon on 27 Apr 2013
Edited by Jan Simon on 27 Apr 2013

What do you expect "handles.axesN" to be? perhaps you want:

axes(handles.(sprintf('axes%d', N)));

5 Comments

Chethan on 29 Apr 2013

Well i tried that code, i'm getting error like undefined variable handles.. let me explain you, this is my calling function FIG5 GUI M-file

        disp('Texture results saved...');
        displayResults('textureResults.txt', 'Texture Results...');
        guidata(hObject,handles)

and displayResults is another separate function.

    function displayResults(filename, header)
    figure('Position',[200 100 700 400], 'MenuBar', 'none', 'Name', header, 'Resize',     'off', 'NumberTitle', 'off');
    % Open 'filename' file... for reading...
    fid = fopen(filename);
    for N=1:5
        imagename = fgetl(fid);
        if ~ischar(imagename), break, end       % Meaning: End of File...
         (x) = imread(imagename);
         axes(handles.axesN);
         imshow(fname);
         xlabel(imagename);
    end
    fclose(fid);

Now this will open separate window to display those images( with subimages and all, I've edited), but I've 5 axes in FIG5 GUI where exactly i need to display these images. FOR loop is must because I've 15 images in another similar GUI. Do i need to remove figure('position', [200 100. . .) line in above code?

Jan Simon on 29 Apr 2013

Please do not post a vague description of the error message, but an exact copy of the complete message. We cannot guess the omitted parts of the message, but they are important.

I do not understand your message. What does "my calling function FIG5 GUI M-file" mean? Is the shown code working or not? Why do you create a figure, when you want to draw to existing figures? Where does "handles" come from in your code? I assume a guidata() is missing here. To which figure should the "handles" belong to?

Chethan on 2 May 2013

well sorry, let me explain clearly. FIG5 is my GUI with 5 axes, with a push button function Texture_Callback(hObject, eventdata, handles). this function calls displayResults('textureResults.txt');. Now i copied displayresults function to FIG5 file/function only. I removed figure('position'. . .) line since i don't need a new window.

    function displayResults(filename)
    % Open 'filename' file... for reading...
    fid = fopen(filename);
    for N=1:1:5
        imagename = fgetl(fid);
        if ~ischar(imagename), break, end       % Meaning: End of File...
        x=imread(imagename);
        axes(handles.(sprintf('axes%d', N)));
        imshow(x)
        %xlabel(imagename);  
    end
    fclose(fid);

Above function is in FIG5 file. Now i'm getting error like

    ??? Undefined variable "handles" or class "handles.axes1".
    Error in ==> Fig5>displayResults at 95
    axes(handles.(sprintf('axes%d', N)));
    Error in ==> Fig5>Texture_Callback at 142
        displayResults('textureResults.txt');
    Error in ==> gui_mainfcn at 96
        feval(varargin{:});
    Error in ==> Fig5 at 42
    gui_mainfcn(gui_State, varargin{:});
    Error in ==>
    @(hObject,eventdata)Fig5('Texture_Callback',hObject,eventdata,guidata(hObject))
    ??? Error while evaluating uicontrol Callback

Hope, now it is clear

Jan Simon
Answer by Image Analyst on 28 Apr 2013

There are only 5, so get rid of the loop and just do it explicitly

% Read in image 1
theImage = imread(filename);
axes(handles.axes1);
imshow(theImage);
% Read in image 2
theImage = imread(filename);
axes(handles.axes2);
imshow(theImage);

and so on.

I suggest you use fullfile(), and exist(filename, 'file') to construct the full filename (folder + base filename + extension) and check to see that it actually exists.

1 Comment

Chethan on 29 Apr 2013

Let me explain you, this is my calling function FIG5 GUI M-file

        disp('Texture results saved...');
        displayResults('textureResults.txt', 'Texture Results...');
        guidata(hObject,handles)
and displayResults is another separate function.
    function displayResults(filename, header)
    figure('Position',[200 100 700 400], 'MenuBar', 'none', 'Name', header, 'Resize',     'off', 'NumberTitle', 'off');
    % Open 'filename' file... for reading...
    fid = fopen(filename);
    for N=1:5
        imagename = fgetl(fid);
        if ~ischar(imagename), break, end       % Meaning: End of File...
         (x) = imread(imagename);
         axes(handles.axesN);
         imshow(fname);
         xlabel(imagename);
    end
    fclose(fid);

Now this will open separate window to display those images( with subimages and all, I've edited), but I've 5 axes in FIG5 GUI where exactly i need to display these images. FOR loop is must because I've 15 images in another similar GUI. Do i need to remove figure('position', [200 100. . .) line in above code?

Image Analyst

Contact us