1. ## Global Variables Trouble.

Hey there.

I'm using GUIDE in Matlab to create a slider, and am trying to store this variable as a global variable so that I can use it in simulink. Is there any way that I can do this? I have written an .m file that says

Code:


global sRT1

sRT1 = sliderValue
(Where sliderValue is already defined in the code before it.)

But this doesnt seem to actually update the variable.

If I manually define the variable in the matlab workspace eg "temperature1 = 34" then the variable shows in simulink as 34. If I run the GUIDE program I have written and move the slider around, the matlab workspace shows that "temperature1 = 45.6" (or whatever the slider value is). However, when I run the simulink program again, the value still shows as 34. (or the previously defined value that I have manually set in the MATLAB workspace).

Sorry for the rather unorganised post, thanks for reading it.

Does anybody have any idea why this is happening?

EDIT:

This might be an insight into why this is happening.

Ignore the bottom 3 sliders, they for when I get the top one working.

2. I think you have to make sliderValue a global variable in the workspace aswell as in your calling function before you can use it there.

Ie type "global sliderValue" at the comand propmt before you start. I am not sure tho, I will look into it more tonight.

Elbarto

3. You, my friend are a god. Just one last thing though.

If I run the simulink model with a simulation stop time of "inf", I have to stop and restart the simulation for the new values to be used if I change the slider value. Is there any way to make the values of the slider 'live' so that the can be updated in the simulink model as I move them on the slider?

Alex

4. I am glad that worked for you - I had a stab in the dark there so wasn't sure if it was good advice or just a plain lie

As far as the simulink model is concerned, I am not familiar with how simulink works but I would assume it uses a handle structure for its objects (basically uses the same principle as matlab's figures/GUI's use) in which case it is possible to do what you want.

Assuming the above is correct,

1) you can pass in the handle to your simulink model into your GUI and store that as a variable using the "guidata" function.
2) every time the slider changes value, you can change the simulink variable by using the "set" command since you will always know the handle to your model.

The advantage of this approach is that it eliminates global variables altogether and is much more maintainable. If you post up your simulink model and your current code with an explanation of how to run it then I might be able to see if there is a way to implement this.

I apologize I cant be more helpful at the moment, I have more of a civil engineering background so have not had any experience with simulink yet nor do I quite understand exactly what its applications are.

Regards Elbarto

5. Thanks, I will give that a try. Here is the Model:

Remember that you have to define the following global variables in the matlab workspace before the model will work.

>> global sliderValue
>> global sliderValue2
>> global sliderValue3
>> global sliderValue4
>> global thermostatValue

Alex

6. Unfortunately your model does not work in my matlab with the instructions you posted (I am running 2008b).

Maybe this link might offer some assistance:

Passing data between Simulink and MATLAB GUI