How to open a userdefined simulink model from a GUI

Hi Guys,
i am working on a GUI that has to get access to simulink models. Now i want the user to choose an existing model when pressing the edit-button! The callback of this edit button is shown here:
function edit__Callback(hObject, eventdata, handles)
% hObject handle to edit_ (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
startingFolder = 'C:\Users\powersyslab\Desktop\Draw'
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, '*.mdl')
[baseFileName, folder] = uigetfile(defaultFileName, 'Select a mdl. file')
if baseFileName == 0
% User clicked the Cancel button.
return;
end
Unfortunately the Simulink model is not opened when i run it this way. I can choose a model but when i doubleclick on it, nothing happens. Thats why i added
open_system('C:\Users\powersyslab\Desktop\Draw\baseFilename','tab')
to the end of the code...I want to start the simulink model by accessing it directly with its name chosen from the user. The name is saved in baseFilename. When i do so i get the error
'C:\Users\John\Desktop\Draw\baseFilename' is not a valid Simulink object name and no matching file found.
When i read baseFilename it says
*baseFileName =
Sample_1.mdl*
How can i make that work? i dont know how to start the model Sample_1.mdl correctly!
Would be glad for your help!
Best regards, John

 Réponse acceptée

Walter Roberson
Walter Roberson le 22 Sep 2015

1 vote

open_system(fullfile('C:\Users\powersyslab\Desktop\Draw', baseFilename), 'tab')

6 commentaires

John
John le 22 Sep 2015
Thank you very much Mr. Roberson:) Works fine!!!
Ohhh, Now i face a similar problem already:(
I would like the user to choose the model that is run in the simulation. This is done by executing the code above. The goal shall be to get the name of the model as a string.
Because afterwards i would like to run the simulation with the command
Simulation('modelname')
Is there a way to get to the string of BasefileName?
Best regards, John
fileparts()
Okay, maybe i am not getting the point, but when i use fileparts() like this
fullFileName = fullfile(folder, baseFileName)
[pathstr,name,ext] = fileparts(fullFileName)
I get the full path, correct! The variable name contains the string of the model i want to execute. The problem for me is, that the model is run with the command Simulation('name_of_model') . So my question is how to extract the models string from the variable name.
name =
Sample_1
So that i can start the simulation with
Simulation('Sample_1')
Many thanks once again!!!
Simulation(name)
John
John le 22 Sep 2015
oha...thanks a lot for your patience! i was a little bit confused! Perfect!!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by