Ozan
Ozan

Reputation: 27

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

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!

handles.baseFileName
open_system('handles.baseFileName/NetworkSelector')

% 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 )

Error using GUI>networkselector_Callback (line 711)
'handles.baseFileName/NetworkSelector' is not a valid
Simulink object name and no matching file found.

Error in gui_mainfcn (line 95)
        feval(varargin{:});

Error in GUI (line 43)
    gui_mainfcn(gui_State, varargin{:});

Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)GUI('networkselector_Callback',hObject,eventdata,guidata(hObject))
- Show complete stack trace

Error while evaluating UIControl Callback.

Upvotes: 0

Views: 695

Answers (2)

Ozan
Ozan

Reputation: 27

As you said, I succeeded to remove the extension by writing the following code inside networkselector_Callback:

[pathstr,name,ext] = fileparts(handles.baseFileName); 
newFilename = fullfile( pathstr, name ); 
str = [newFilename,'/NetworkSelector'] 
open_system(str) 
guidata( hObject, handles )

Also it worked. Thank you very much Phil :))

Upvotes: 0

Phil Goddard
Phil Goddard

Reputation: 10772

As per the error message, you don't have a subsystem (literally) called 'handles.baseFileName/NetworkSelector'.

Presumably what you really want is to open the subsystem called [handles.baseFileName,'/NetworkSelector'], that is, using the name of the model stored in handles.baseFileName.

I would suggest that for robustness you also need to wrap the appropriate parts of the code inside a check (using bsIsloaded) to ensure the model is indeed open before trying to open the subsystem.

Upvotes: 1

Related Questions