How to pass new value of parameter from GUI into .m script?
6 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Adam
2017-3-3
编辑:Adam
2017-3-3
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 个评论
Adam
2017-3-3
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'!
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!