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

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.

 채택된 답변

Adam
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

Why it doesn't make sense? I have a script about 200 lines, where I have about 15 values I need to change and I'am calling some other functions. So you think it's better to make another fuction where I will put all values I need to change with GUI and let the script just load outputs of this function? I tried this approach now. I have created function which will give me output as "value":
function [value] = myFunc(number)
value = number;
end
I want to use "value" in the script like this:
variable(1:10) = NaN;
for i = 1:10;
variable(i) = i * value;
end
figure
plot(variable)
So editbox looks like this:
function edit1_Callback(hObject, eventdata, handles)
number = str2double( get( hObject, 'String' ) );
myFunc( number );
And i added one more push button to load this number into Matlab workspace:
function pushbutton1_Callback(hObject, eventdata, handles)
myFunc
And pushbutton2 just execute main script:
function pushbutton2_Callback(hObject, eventdata, handles)
evalin('base','script')
Executing script works fine, but loading input number with function not. If I put into edit box for example value 10 and pres pushbutton1 I get this error:
Not enough input arguments.
Error in myFunc (line 3)
value = number;
Error in gui>pushbutton1_Callback (line 81)
myFunc
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
myFunc in my example was just a quick example of calling a function with the value passed in. I intended that to be a function replacing your script, but a lot depends what you are doing.
Programming with scripts in general only really makes sense for initial testing of ideas. Once you start wanting to change parameters you should be using functions instead as scripts do not lend themselves to being run on multiple ranges of parameters.
Judging by the number of questions we get on here that use things like
evalin( 'base',... )
I assume this is what gets taught on Matlab courses though I have no idea why, it is a ghastly way to program something.
Is all the other data defined within your script or does it expect paramaters to already exist in the the workspace when it runs? This is the biggest problem with scripts, they do not define their own sealed workspace so just looking at a script you have no idea whether it expects certain variables to already exist before it is called.
If it is self-contained then you can just put a function signature on the top of it as e.g.
function myFunction( number )
...
% Your script code here
...
end
Now instead of a script you have a function that takes the argument you want as input. You need no code at all in your edit box callback if you only intend the script/function to run when you press the pushbutton. And there simply use
number = str2double( get( handles.edit1, 'String' ) );
myFunction( number );
where myFunction is, as described above, your script file turned into a function.
If you want to pass in 15 parameters then put these together in a struct (or a class object) and pass that in rather than trying to pass in 15 independent arguments. Your example of just the one edit box suggested you only had 1 variable though.
So I tried it exactly as you wrote:
function myFunction(number)
variable(1:10) = NaN;
for i = 1:10;
variable(i) = i * number;
end
figure
plot(variable)
end
Edit button where i want set value of number:
function edit1_Callback(hObject, eventdata, handles)
number = str2double( get( handles.edit1, 'String' ) );
myFunction( number )
Push button:
function pushbutton1_Callback(hObject, eventdata, handles)
myFunction
And I'am still getting this error with no result:
Not enough input arguments.
Error in myFunction (line 6)
variable(i) = i * number;
Error in GUI>pushbutton1_Callback (line 81)
myFunction
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
What am I doing wrong?
Actually I wasn't attending any Matlab course, but I'am self studying it, but right now I'm stucked on creating that GUI for my school project. Structure of script, where I wanted to use procedure I was asking about and you are showing me on example script, is below:
% loading some data
load('prepared data1')
load('prepared data2')
% setting up all necessarily parametres
parameter1 = value1;
.
.
parameter15 = value2;
% some minor calculation with some parametres
valueX = (parameter1-paramater2)/parameter8; % example
% main cycle
for i = 1:100000 % i+1
calling functions with callculations % eg. valueY = function1(parameter1,parameter9)
some if conditions
calculations using outputs of functions
end
% showing results
disp(['Result': sum of valueY])
plot(valueY)
So, I want to set up values of all parametres with that GUI and run it. I'd like to have all "parametres", "values", "results of calculations" in Matlab workspace to check them. Anyway, thanks you for your time.
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'!
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

질문:

2017년 3월 3일

편집:

2017년 3월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by