How to pass new value of parameter from GUI into .m script?
5 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Adam
on 3 Mar 2017
Edited: Adam
on 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 Comments
Adam
on 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'!
More Answers (0)
See Also
Categories
Find more on Migrate GUIDE Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!