MATLAB Answers

Communication between MATLAB code and GUI

3 views (last 30 days)
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?

  0 Comments

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 30 Mar 2020
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

Show 7 older comments
Geoff Hayes
Geoff Hayes on 31 Mar 2020
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.
Miroslav Jiránek
Miroslav Jiránek on 31 Mar 2020
FINALLY its working. Thank you so much for your advices and patience

Sign in to comment.

More Answers (0)

Sign in to answer this question.