Communication between MATLAB code and GUI

Hello all,
Maybe I have very simple question, but I can't find out how to start communicate my programme in MATLAB with GUI. GUI looks like this. I placed the background image of the crossroad in GUI, where the whole simulation should run
function background_CreateFcn(hObject, eventdata, handles)
bg = imread('C:\Users\Miroslav\Desktop\Crossroad\cross_matl.png'); imagesc(bg);
uistack(bg, 'bottom');
At the beginning of the matlab program, I initialize the images of cars which has to be moved on the crossroad in GUI.
openfig('C:\Users\Miroslav\Desktop\Crossroad\GUI_final.fig'); %opening GUI
hold on
%----------------------DOWN -> UP --------------------------------------
car_9=imread('C:\Users\Miroslav\Desktop\Crossroad\blue_car.png');
car_10=imread('C:\Users\Miroslav\Desktop\Crossroad\red_car.png');
car_11=imread('C:\Users\Miroslav\Desktop\Crossroad\white_car.png');
car_12=imread('C:\Users\Miroslav\Desktop\Crossroad\yellow_car.png');
CAR_9 = Move_DU(); %deklaration objects to the class
CAR_10 = Move_DU();
CAR_11 = Move_DU();
CAR_12 = Move_DU();
DU = [CAR_9,CAR_10,CAR_11,CAR_12];
car_9_img = imagesc('CData',car_9,'XData',DU(1).getCurrentX(),'YData',DU(1).getCurrentY()); %loading images GUI
car_10_img = imagesc('CData',car_10,'XData',DU(2).getCurrentX(),'YData',DU(2).getCurrentY());
car_11_img = imagesc('CData',car_11,'XData',DU(3).getCurrentX(),'YData',DU(3).getCurrentY());
car_12_img = imagesc('CData',car_12,'XData',DU(4).getCurrentX(),'YData',DU(4).getCurrentY());
du = [car_9_img,car_10_img,car_11_img,car_12_img];
But when I start the program, images of cars makes their own figure, where they start moving
So.. My question is. How to plot images of cars on the background in the GUI?

 Accepted Answer

Miroslav - is the "only" difference between https://www.mathworks.com/matlabcentral/answers/511717-image-rendering-in-gui and this question is that your crossroads is now a background image? If so, why not create an axes in your GUI where you place the crossroads image and then use that same axes to add the cars? (In a similar fashion as to what you did in the other question.)

10 Comments

I've already created an axes, its called background and I paste the code in my topic. This is writen in GUI section
function background_CreateFcn(hObject, eventdata, handles)
bg = imread('C:\Users\Miroslav\Desktop\Crossroad\cross_matl.png'); imagesc(bg);
uistack(bg, 'bottom');
But how can I plot the cars from the main programme into this axes in GUI?
MIroslav - so you have access to the handle to the axes via handles.axes1 or some other field? So that you could do something like
car_9_img = imagesc(handles.axes1, 'CData',car_9,'XData',DU(1).getCurrentX(),'YData',DU(1).getCurrentY());
My axes seems that doesnt have created handle. Can you suggest me how to created it?
function background_CreateFcn(hObject, eventdata, handles)
% hObject handle to background (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
bg = imread('C:\Users\Miroslav\Desktop\Crossroad\cross_matl.png'); imagesc(bg);
Well you would have to create it in GUIDE. Unless background your axes? If that is the case, then you would use handles.background to make use of the axes. As for the code you have in the background_CreateFcn, I would put that in your GUI OpeningFcn instead. And I would put the remainder of that code there too
car_9=imread('C:\Users\Miroslav\Desktop\Crossroad\blue_car.png');
car_10=imread('C:\Users\Miroslav\Desktop\Crossroad\red_car.png');
car_11=imread('C:\Users\Miroslav\Desktop\Crossroad\white_car.png');
car_12=imread('C:\Users\Miroslav\Desktop\Crossroad\yellow_car.png');
CAR_9 = Move_DU(); %deklaration objects to the class
CAR_10 = Move_DU();
CAR_11 = Move_DU();
CAR_12 = Move_DU();
DU = [CAR_9,CAR_10,CAR_11,CAR_12];
car_9_img = imagesc('CData',car_9,'XData',DU(1).getCurrentX(),'YData',DU(1).getCurrentY()); %loading images GUI
car_10_img = imagesc('CData',car_10,'XData',DU(2).getCurrentX(),'YData',DU(2).getCurrentY());
car_11_img = imagesc('CData',car_11,'XData',DU(3).getCurrentX(),'YData',DU(3).getCurrentY());
car_12_img = imagesc('CData',car_12,'XData',DU(4).getCurrentX(),'YData',DU(4).getCurrentY());
du = [car_9_img,car_10_img,car_11_img,car_12_img];
Looking more closely at what you have done, you have a script that opens the GUI but then does all this other work outside of the GUI. You don't need a separate script and you should be able to add all of your code to the GUI OpeningFcn or callbacks as needed.
I'm sorry but I think I don't get what you're writing. I can't add all of these images to GUI part, beacuse I'm referring on these images many time in my script in MATLAB - I'm changing the postion of vehicles every cycle like this
set(lr(n),'XData',LR(n).getCurrentX()); %update coordinates
And when I put all of these images to the GUI, they cannot be found in the script. Maybe I still dont get how to create a handle on the background_CreateFcn.
And I also don't get what you're writing about putting code in OpeningFcn. Do I have to pute there the background image of the crossroad, or other images of cars?
Hi Miroslav - that might be part of the problem: trying to reference images/objects in your script that you wish to draw on an axes in your GUI. If you can determine the handle to axes from your script, then you might be able to continue as you are doing now. Perhaps try
hFig = openfig('C:\Users\Miroslav\Desktop\Crossroad\GUI_final.fig'); %opening GUI
handles = guidata(hFig);
hAxes = handles.background;
hold on
% etc.
and you can then use hAxes as the parent axes for your cars (via imagesc).
As for OpeningFcn, I typically use this to initialize the GUI. It is here where I would set the background and do other GUI start up activities. Though you may not find this necessary...
It still seems that my background_CreateFcn still doesn't have a handle. matlab even writes that it doesn't have a handle by itself when I generate Create_Fcn. How can I do so?
function background_CreateFcn(hObject, eventdata, handles)
% hObject handle to background (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
bg = imread('C:\Users\Miroslav\Desktop\Crossroad\cross_matl.png'); imagesc(bg);
% Hint: place code in OpeningFcn to populate background
I'm not sure what you mean by matlab even writes that it doesn't have a handle by itself when I generate Create_Fcn. What error message are you seeing? What code generates the error? If background is your axes object, then just move this code to populate it to your OpeningFcn (which the hint is telling you to do) where the handle to this object will exist. This function should look something like
% --- Executes just before GUI_final is made visible.
function GUI_final_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to GUI_final (see VARARGIN)
% Choose default command line output for GUI_final
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes untitled1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% <---------- add this code ----------->
bg = imread('C:\Users\Miroslav\Desktop\Crossroad\cross_matl.png');
imagesc(handles.background, bg);
Note that how you use the axes handle in imagesc depends upon the version of MATLAB that you are using. And if background is the handle to the axes, then your script will need to know about it so that it can add and move the cars on this background OR move your logic from the script into the GUI OR change your script into a function which takes inputs (current car positions, velocities, etc.) and then returns the updated car positions, velocities, etc.
FINALLY its working. Thank you so much for your advices and patience

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!