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?

조회 수: 8 (최근 30일)
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 )

답변 (1개)

Abhilash Padma
Abhilash Padma 2019년 7월 29일
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
OK
OK 2019년 7월 29일
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 )

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by