- how the static text boxes display strings and not numerics.
- I'm finding the static textboxes through their given Tag properties (i hope you did mean this, and not their variable names defined in another function) in the current figure (gcf))
- For future reference, if you don't want to use gcf and want to look at a particular figure, you can try do do:
Updating variable matrix values to static boxes in GUI !
2 次查看(过去 30 天)
显示 更早的评论
Iam completely new to GUI and programming any correction to code is appreciated !
I am trying to assign the value of variable x in another matfile named "main_file_Run" to static boxs !
For eg: varible x =[32, 7] and keeps updating and i when i press pushbutton it should run the matlab file needs to assign the values upated in x matrix i.e 32 nd 7 to two respective static boxes with tag names text3 and tex4 respectively.
function pushbutton1_Callback(hObject, eventdata, handles)
% a= get(handles.main_file_Run(variable x),'string');
% (set(handles.text3,'string'))
main_file_Run();
I know i completely missed the logic here ! I am only able to run the program by calling the matfile but have no clue what to do to update the values from the variables x to the two static boxes .Firstly how do we load the varible ? should we save it as text file then kind of load into the call back of pushbutton ?
thanks in advance!
回答(1 个)
Kevin Phung
2019-5-3
编辑:Kevin Phung
2019-5-3
make sure your function main_file_run() actually has an output, so in that function:
function x = main_file_run()
% blah blah blah
x = (some answer from calculations above)
end
then in your pushbutton callback:
function pushbutton1_Callback(hObject, eventdata, handles)
x = main_file_Run();
set(findobj(gcf,'Tag','text3'),'String',num2str(x(1)))
set(findobj(gcf,'Tag','text4'),'String',num2str(x(2)))
end
note :
f = findobj(groot,'Name','FigureTitle') % returns figure handle of figure with specified name
16 个评论
chamant swarup
2019-5-3
i did not understand the usage of gcf in this problem !
Firstly my main_file_Run looks like this , on simlation it outputs the two values of X = [ 32 7 ]
these values keep updating until one final optimal value is reached . since these values will be saved to workspace as variable x , my idea was to pull those numbers and update in two static boxes with tag names text 3 and 4 by that i mean their name in property inspector used for manipulations. so do u still mean the same ? will the above code work ?
function main_file_Run
clear
close all;clc;format compact;
global ASC
%global iter
ASC=0;
%iter=0;
fun = @economic_analysis; %fitness calculation function
nvar = 2; % A 2-D problem
lb = [20 4]; % lower Bounds for solar, and battery respectively
ub = [35 8];% upper bounds
% ps = parallel.Settings;
% ps.Pool.AutoCreate = false;
options = optimoptions(@particleswarm,'OutputFcn',@pswplotranges);
options = optimoptions('particleswarm', 'UseParallel', false , 'UseVectorized', false,'MaxIterations',10000);
rng default % For reproducibility
[x,fval,eflag,output] = particleswarm(fun,nvar,lb,ub,options)
% for winter
m1=1;
plot_statistics(x,m1,1)
% for summer
m1=11521;
plot_statistics(x,m1,0)
%for Rest ofthe season
m1=5665;
plot_statistics(x,m1,0)
end
Rik
2019-5-3
They keep updating where? The main_file_Run has no output arguments, so it doesn't return anything.
Why are you using a global?
Why are you starting your function with clear? It doesn't serve any purpose, since the workspace is already clean.
Why are you closing all windows and setting the format? You have at least a GUI open that might close because of this. I don't know if any of your current functions output something to the command window, but from the looks of it, none should.
chamant swarup
2019-5-3
OK ! i will have to explain this in detail , this is an optimization algorithm where it returns the right set of values in search space satisfying the constrainsts set. The algorithms returns the optimum values after the simulation is complete . There are several other sub functions , i just copy pasted the main file . You can have a look at other files attached below.
In nutshell, i want to assign optimum values obtained at the end of the simulation to a static boxes . the values are updated like shown below .
Rik
2019-5-3
You should make that variable x an output of one of those functions. Then you can easily set it. Just like Kevin put in his answer.
chamant swarup
2019-5-3
what about the usage of gcf ? why are we using it to transfer data from x to static box ?
function pushbutton1_Callback(hObject, eventdata, handles)
x = main_file_Run();
set(findobj(gcf,'Tag','text3'),'String',num2str(x(1)))
set(findobj(gcf,'Tag','text4'),'String',num2str(x(2)))
end
Rik
2019-5-3
As you're probably using GUIDE, you don't need to use findobj to get the handles. The gcf function returns the handle to the current figure, as its documentation would have told you. Then the findobj function will return the handles of objects that satisfy certain conditions.
In this case you can probably use the code below instead:
function pushbutton1_Callback(hObject, eventdata, handles)
x = main_file_Run();
set(handles.text3,'String',num2str(x(1)))
set(handles.text4,'String',num2str(x(2)))
end
Kevin Phung
2019-5-3
Rik, thanks for the help explaining the reasoning behind my answer. I didnt notice that chamant was using GUIDE.
Chamant, I suggest you read into how to use a script vs a function. When you're running functions, they each have their own 'scope' of variables that are not necessarily the same as your base workspace. Think about how hectic it would be having to check if your variable 'a' defined in a script (base workbase) is consistent across all functions within your script. so having :
clear
close all;clc;
at the beginning of a function is a bit redundant. because at the start of any function, the workspace is already clear. in this case for callback functions, it's actually detrimental. there are two default inputs: object handle (That triggered the callback) and the 'event.' You would not want to clear those.
Anyway, to reiterate, the solution to your dilemma is to set an output for your function main_file_run() and to add the 3rd argument to your set() function which you did not include.
Rik
2019-5-4
Thanks for the insights kevin! Unfortunately the values still are not pulled into the static boxes !
Here is what i did
function X= main_file_Run
X =[x(1),x(2)];
end
and in call back section i wrote
function pushbutton1_Callback(hObject, eventdata, handles)
X = main_file_Run();
set(handles.text3,'String',num2str(X(1)))
set(handles.text4,'String',num2str(X(2)))
The even more strange thing is when i end the function there are many errors relating varfile but without end it works but not able to pull the values of X !
Rik
2019-5-4
I hope that is not the full contents of the main_file_Run function, because then x would not yet be defined. The callback looks like it is fine if you put it exactly like this.
About the end keyword: GUIDE is so old it is from the time where the convention was to leave your function unclosed. Over the last years this convention has flipped (e.g. to enable the use of nested functions). The two styles are incompatible, so either you need to use an end for every function in an m-file, or for none of them.
chamant swarup
2019-5-4
Thanks for explanation end keyword rik !
I am getting following errors ? how should i tackle that ?
Rik
2019-5-4
Do you still have the close in your function? Because that will close the GUI, which will delete the object, causing this error. Otherwise, try closing all GUI windows and reopen it to force a refresh.
chamant swarup
2019-5-4
no , when i closed the specific function it asked me to close all the nested functions iniside but the number of errors doubled this time , i have no clue
function varargout = GRID_INTERFACE(varargin)
% GRID_INTERFACE MATLAB code for GRID_INTERFACE.fig
% GRID_INTERFACE, by itself, creates a new GRID_INTERFACE or raises the existing
% singleton*.
%
% H = GRID_INTERFACE returns the handle to a new GRID_INTERFACE or the handle to
% the existing singleton*.
%
% GRID_INTERFACE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GRID_INTERFACE.M with the given input arguments.
%
% GRID_INTERFACE('Property','Value',...) creates a new GRID_INTERFACE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before GRID_INTERFACE_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to GRID_INTERFACE_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help GRID_INTERFACE
% Last Modified by GUIDE v2.5 03-May-2019 13:25:58
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @GRID_INTERFACE_OpeningFcn, ...
'gui_OutputFcn', @GRID_INTERFACE_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before GRID_INTERFACE is made visible.
function GRID_INTERFACE_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to GRID_INTERFACE (see VARARGIN)
% Choose default command line output for GRID_INTERFACE
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes GRID_INTERFACE wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = GRID_INTERFACE_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
dict=[0,1200,2500,3500,4000];
str= sprintf('Consumption = %.d Kwh/annum',...
dict(get(handles.popupmenu1,'value')));
set(handles.text2,'String',str);
end
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
end
% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu2
end
% --- Executes during object creation, after setting all properties.
function popupmenu2_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
end
% --- Executes on selection change in popupmenu3.
function popupmenu3_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu3 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu3
end
% --- Executes during object creation, after setting all properties.
function popupmenu3_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
end
% --- Executes on selection change in popupmenu4.
function popupmenu4_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu4 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu4
end
% --- Executes during object creation, after setting all properties.
function popupmenu4_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% a= get(handles.main_file_Run(variable x),'string');
% (set(handles.text3,'string'))
X = main_file_Run();
set(handles.text3,'String',num2str(X(1)));
set(handles.text4 ,'String',num2str(X(2)));
end
% 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)
% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1
end
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
end
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
end
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
end
end
Rik
2019-5-4
You were inconsistent with where you put the end keywords. I fixed those in the version that I attached as an m file.
chamant swarup
2019-5-4
编辑:chamant swarup
2019-5-4
thanks i have run the simulation will have to check if the output will read into the static boxes !
will get backafter the simulation is complete !
But as soon as i hit the push button the window disappears running the matfile ! will have to see if pops up again after the simulation is complete . I just finished the simulation these errors again showed up and data still not captured into the static box .
Error using matlab.ui.control.UIControl/set
Invalid or deleted object.
Error in GRID_INTERFACE>pushbutton1_Callback (line 158)
set(handles.text3,'String',num2str(X(1)));
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in GRID_INTERFACE (line 38)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)GRID_INTERFACE('pushbutton1_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating DestroyedObject Callback.
Rik
2019-5-4
Somewhere some function is closing your GUI. You can use the debugger to set a breakpoint and go through your code line by line to see where your GUI is being closed.
chamant swarup
2019-5-5
X = main_file_Run();
handles.text3.String = num2str(X(1));
handles.text4.String = num2str(X(2));
I changed the code to this and it worked fine !!
thanks for the help people !!
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)