How to pass new value of parameter from GUI into .m script?

5 visualizzazioni (ultimi 30 giorni)
Hello, I want to make simple GUI for my script, where I can edit parameter values and running that script.
I've created example scipt and GUI with 2 buttons. I'cant put script code into GUI code, I will need to aply it on much larger script.
So, example script code:
number = 10;
variable(1:10) = NaN;
for i = 1:10;
variable(i) = i * number;
end
figure
plot(variable)
Push button code which runs the script, that is working fine. "script" is name of .m file, not function:
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
evalin('base','script')
But I dont know what to type into edit button code If i want to change value of "number" in the script:
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
And last thing, sometimes when I try to plot more graphs, one figure overwrites GUI figure and I can see only buttons, but not whole GUI.
Thank you fot any help. I was asking similar question several days ago, but I deleted it by mistake.

Risposta accettata

Adam
Adam il 3 Mar 2017
Modificato: Adam il 3 Mar 2017
Use a function instead of a script and it is trivial
number = str2double( get( hObject, 'String' ) );
myFunc( number );
It doesn't make much sense to be combining GUIs with scripts.
  5 Commenti
Adam
Adam il 3 Mar 2017
You need to move that code out of your edit callback and into your pushbutton callback. You only need code in a callback if you want something to happen in response to changing the edit box value. In your program, as I understand, that isn't the case. You can just change the value and nothing should happen. Then when you press the pushbutton its callback will pickup the value from the edit box, pass it to your function and call it.
I'm not sure where your example script requires a parameter from the GUI since it is loading data in. Is it the 15 parameters that it wants to get from the GUI?
There are numerous things I might add regarding your script though it is unclear exactly what all the parameters would be. I don't wish to pile in too much information to just confuse you more.
I assume the names parameter1,...parameter15 are just an example as you should never name variables like this. If they really are just all generic parameters then put them in an array. If they each have some individual meaning give them a name that conveys that. If they need to be passed around all together put them on a struct instead of individual variables e.g.
myStruct.parameter1 = 7;
myStruct.parameter2 = 15;
though again, I just use your example - never name them 'parameter1', 'parameter2'!
LamaObecna
LamaObecna il 3 Mar 2017
Modificato: LamaObecna il 3 Mar 2017
Finally, example script is working, hurey :) How can I see value of inserted number and calculation of variable in the matlab workspace?
To the main script. It's really just example with names to be clear what they are. Loaded data are tables with several thousands values of temperature, power consumption etc. They change depending on value of "i" in for cycle.
Parametres specifie how simulation will look like. If parameter1 is for example 20 and parameter2 is 50, result of simulation will be different from parameter1 = 30 and parameter2 = 40. Same for other parametres. Now, If I want to simulate different conditions for same loaded data I have to rewrite parameters directly in the script. Thats not much user friendly for someone who sees that script for the first time. Thats the reason fo GUI. User will define his parametres and run simulation without touching main script/function. Functions doesnt use every parameters, some use 2, some use 4 etc, so they dont have to be all together for each function. How to load them from GUI in some nice way?
EDIT: I dont understand it. If I make new GUI.fig with push button and editt button and code in push button:
number = str2double( get( handles.edit1, 'String' ) );
myFunction( number )
GUI is working, but If I shut down Matlab, open it and run GUI again I get this error:
Struct contents reference from a non-struct array object.
Error in GUI>pushbutton1_Callback (line 81)
number = str2double( get( handles.edit1, 'String' ) );
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in GUI (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)GUI('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by