How exactly should I use open_system to open a Simulink block by using a call operation with 'handles' instead of a particular Simulink file name in GUI?

11 views (last 30 days)
Hello people,
I am stuck in my following code because I cannot find out any solution to make a Simulink object, defined in 'open_system', valid in a push button tagged 'networkselector' of my GUI. 'handles.baseFileName' is the chosen file, which I succeeded to open by clicking another push button in an open file window, but I don't know how to use the call operation with 'handles' in 'open_system' correctly to be able to open the Simulink block named 'NetworkSelector' in that file. Also I hope that you help me in this issue.
Thank you very much in advance!
% push button to open a Simulink file
function open_file_Callback(hObject, eventdata, handles)
startingFolder = 'C:\Users\xxx\Documents'
if ~exist(startingFolder, 'dir')
% If that folder doesn't exist, just start in the current folder.
startingFolder = pwd;
end
% Get the name of the mat file that the user wants to use.
defaultFileName = fullfile(startingFolder, '*.slx')
[handles.baseFileName, folder] = uigetfile(defaultFileName, 'Select a Simulink file')
if handles.baseFileName == 0
% User clicked the Cancel button.
return;
end
fullFileName = fullfile(folder, handles.baseFileName)
[name] = fileparts(fullFileName)
open_system(fullfile('C:\Users\xxx\Documents', handles.baseFileName), 'tab')
guidata( hObject, handles )
% push button to open a Simulink block named NetworkSelector
function networkselector_Callback(hObject, eventdata, handles)
handles.baseFileName
open_system('handles.baseFileName/NetworkSelector')
guidata( hObject, handles )

Answers (1)

Abhilash Padma
Abhilash Padma on 29 Jul 2019
Hi,
I understand that you want to open a block of a model from GUI and you are using open_system function. In order to open a block of a model, the model should be loaded into memory first. I thinkNetworkSelector” is the name of the block, so you should append the string “/NetworkSelector” to the variable “handles.baseFileName” . Use the following two lines of code in networkselector_Callback function which may solve your problem.
load_system(handles.baseFileName);
open_system([handles.baseFileName /NetworkSelector]);
You can refer the following links for more information:
  1 Comment
OK
OK on 29 Jul 2019
Hey,
firstly thank you very much for your answer to try to solve my problem. I found out the solution by getting suggestions in StackOverFlow before, which I want to share with you and other Mathworks users as well.
First step: I removed the extension of the chosen file, otherwise it wouldn't be possible to adress the Simulink subsystem properly. I mean that handle.baseFileName should be called f.e. 'SimulinkFile/NetworkSelector' instead of 'SimulinkFile.slx/NetworkSelector'. This following code example let me do it:
[pathstr,name,ext] = fileparts(handles.baseFileName);
newFilename = fullfile( pathstr, name );
Second step: I used open_system like you suggested in your answer, but I did it by giving a variable named 'str' in the following code, because it wouldn't work otherwise:
str = [newFilename,'/NetworkSelector']
open_system(str)
Also the whole solution inside 'networkselector_Callback' seems like:
function networkselector_Callback(hObject, eventdata, handles)
% hObject handle to networkselector (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA
[pathstr,name,ext] = fileparts(handles.baseFileName);
newFilename = fullfile( pathstr, name );
str = [newFilename,'/NetworkSelector']
open_system(str)
guidata( hObject, handles )

Sign in to comment.

Categories

Find more on Simulink Functions 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!