I have a situation where I create a waitbar, read a large image and plot the image to the gui axes. However what happens is that the plot is created inside the waitbar instead.
hWait = waitbar(0,'1','Name','Reading calibration file ...');
% why do I need the '1' in waitbar ???
readCalibrationImage( handles );
% delete( hWait );
set(handles.figure1,'CurrentAxes',handles.axesROI)
plotROIImage( handles, imagedata, roi, lineV, lineH, doExportPlot )
only if I delete the handle the plot is created in the correct window.
How do I enfore plotting to the correct window?
Try putting a
set(0, 'CurrentFigure', handles.figure1);
in front of the CurrentAxes statement. AFAIK, set(...,'CurrentAxes') does not automatically switch the active figure...
Related
I am going to save an image in the GUI to a sperate figure. Some of codes relevant are as follows:
axes(handles.axes1); %axes object
subplot(131); imshow(tempData(:,:,1),[]); title('I1');
subplot(132); imshow(tempData(:,:,2),[]); title('I2');
subplot(133); imshow(tempData(:,:,3),[]); title('I3');
%The three images are displayed in the GUI
% saved to a new figure
handles.axes1
figurenew = figure;
copyobj(handles.axes1,figurenew);
Then, there is an error when running the code:
Error using copyobj
Copyobj cannot create a copy of an invalid handle.
Does that means the handle handle.axes1 no longer exist? Then how to modify the codes to save the displayed image in the GUI?
Each subplot has its own Axes object.
You can get this Axes object by writing as follow.
figure;
axes1 = subplot(131);
Then you can copy the object as you wrote.
figurenew = figure;
copyobj( axes1, figurenew );
Have an existing image in which I am overlaying a contour as per below
imshow(I), title(sprintf('contour')), hold on, contour (thresI,'w');
Would it be possible to overlay the contour on the image without calling figure? Would like to export the resulting image (into png/jpg) without calling figure()/imshow if possible.
Simply put: no. What you probably want though, is to save the figure without plotting. Use the 'visible' switch:
figure;
set(gcf,'visible', 'off');
Imshow(I)
(..) more of your plots
You could call the figure window so that it's not on the screen:
scrsz = get(groot,'ScreenSize');
figure('Position',[-scrsz(4)/2 -scrsz(4)/2 scrsz(3)/2 scrsz(4)/2])
so no one will (probably) see it
An other Option is of course imwrite
My interest is to display image in axes, I've 2 GUI's input_window and fig5.
In my former GUI I've used one axes to display image using uigetfile ,
axes(handles.axes1);
imshow(fname);
Now by pressing pushbutton in 1st GUI it switches to fig5 GUI, where I've used so many axes to display multiple images, also i want to display fname image in one axes and it should automatically get displayed in fig5 GUI. For this I've used same above codes in Fig5_OpeningFcn
axes(handles.axes1);
imshow(fname);
I'm getting error like Undefined function or variable 'fname'.
please help me how to pass variables between GUI's
You could use setappdata and getappdata, like so:
% In the first figure:
setappdata(0, 'fname', fname);
% Show the second figure...
% In the second figure:
fname = getappdata(0, 'fname');
% Clear the appdata
setappdata(0, 'fname', '');
% Do stuff with fname
I need a help with MatLab GUI .
I have a GUI with an axes on it,
and a function plotData(axes,data) which has axes as parameter.
The GUI has a button "plot Data".
How can I do the following:
When the button is clicked, call the function plotData with the parameter axes1 and the data that I want to plot?
I want the plot to be directed to axes1 which exists in the GUI.
It's suppose to be simple, but when I send the axes as parameter it doesn't plot on the GUI, or maybe it does but I cant see it.
It works fine for me without the function: just to plot the data. but to plot the data it's not 1 row :).
i tried calling ax which storing the GUI's axes handle on different M file, but since i call it as a function from different M file nothing happens with the GUI axes handle but it also not returning any error.
Side remark: Your question is a bit unclear: if you added small code snippet to illustrate what you have tried, better answers could be provided.
To the question at hand:
Have you tried directing the plot to axis1 in plotData?
function [] = plotData( ax, data )
% make ax the current axes for plot
axes( ax );
% continue with plotting the data
% ...
You can achieve the effect of axes( ax ); in a more efficient way through the specific plot commands you are using.
For example, if you are using simple plot
plot( ax, data ); % plots data to axes ax
check the documentation of the specific plot command you are using for the axes argument.
With Guide I made a Matlab Gui that have 10 Axes in which i want to display images, in all of them at once, after i press a Button.
I made a separate .m file with function Load_Write_Img_Results(img_index) that i call from the Button Callback which have the following code:
for i = 1 : 10
handles_imgOut = findobj('Tag', ['imgOut' num2str(i)]);
set(handles_imgOut, 'HandleVisibility', 'ON');
axes(handles_imgOut);
image(imgs_data{img_index(i)});
...
end
Every time when i run the main Gui and press the button for the first time, images are displayed in all axes, so everything works ok.
The problem appear when i press the button for the second time and i get this error:
Error using axes
Invalid object handle
on this line:
axes(handles_imgOut);
When debugging, i saw that after handles_imgOut = findobj('Tag', ['imgOut' num2str(i)]); the handles_imgOut is not getting any value and is empty, so it's obvious that error.
Is there any chance i can't get handles for axes at the second press of the button?
Also, i want to know how can i solve this warning from Matlab:
Calling AXES(h) in a loop can be slow. Consider moving the call to AXES outside the loop.
Thanks in advance, any suggestions are welcome!
[SOLUTION]:
for i = 1 : 10
handles_imgOut = findobj('Tag', ['imgOut' num2str(i)]);
set(handles_imgOut, 'HandleVisibility', 'ON');
axes(handles_imgOut);
image(imgs_data{img_index(i)});
set(gca, 'Tag', ['imgOut' num2str(i)]); //! renew the tag
...
end
I'm fairly new to GUIDE and I've encountered similar problems, with the graph not being updated more than once / axes not found.
In the end, I used the following approach, which I hope can be useful to you too:
% I get my axes directly from the handles object, instead of using findObj:
graph1 = handles.axes1;
graph2 = handles.axes2;
% clear axes
cla(graph1);
cla(graph2);
% showTrial is my function that draws the graphs -
%notice that I used handles to store other variables as well (.data, .trials)
showTrial(handles.data, handles.trials(1), graph1, graph2)
To sum it up:
don't use findObj, get your axes from handles (it should automatically contain them as imgOut1,imgOut2, etc.)
pass the axes to your drawing function or pass directly the handles variable e.g. Load_Write_Img_Results(img_index, handles)
The fact that it works on the first button press, but not thereafter hints that
image(imgs_data{img_index(i)});
opens new axes instead of drawing into the existing ones. Since the new axes are not initialized with your tags, findobj will not find them. Either
(1) make sure to hold the original axes by placing a hold on command right after their creation,
or
(2) renew the tag right after the image command by
set(gca, 'Tag', ['imgOut' num2str(i)]);