現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
How To Always Updating Data From Function To GUI In Matlab?
4 ビュー (過去 30 日間)
古いコメントを表示
QY Goh
2012 年 5 月 19 日
Hello everyone;
Nice to meet ur guys,i am new user in matlab and i currently doing some assignment by using matlab. I wish to knew how can i always update the value in the "edit text" function in Matlab Gui. Seem the value can always updating when in the matlab command window but it can't update in the GUI.Has ur guy has any idea about it?Can share it with me. Thank You and has a nice day. (^.^)
p/s:sorry for my bad English grammar.
回答 (1 件)
Jan
2012 年 5 月 19 日
Updated strings and other properties are displayed after a drawnow or pause command.
14 件のコメント
QY Goh
2012 年 5 月 20 日
But i try it already. It still unable to always update the value. My Code is list as below:
global x1
set(handles.Thumb,'string',x1(1));
drawnow();
with x1 is an array in the function and i call it in gui for always update it.isn't ok i set it as global?
Walter Roberson
2012 年 5 月 20 日
What data type is x1? If it is a character string, then x1(1) would be a single character. If x1 is a numeric value, then the String you provide should be sprintf() of the value or num2str() of the value. The syntax you are using is fine, though, if x1 is a cell array of strings.
There is no big problem in making x1 a global, but you do need to declare it as global in each routine in which x1 is used. You would normally instead pass the value as a parameter to your update routine.
QY Goh
2012 年 5 月 20 日
My x1 array is in the numeric in double format. Unfortunately, i still unable to update value inside the array to my Gui. Suppose the syntax didn't has any error. And i quite confuse about it. Because the value in the array x1 will keep changing so the value display on the GUI must change according to the value in x1 as well. (T.T)
Walter Roberson
2012 年 5 月 20 日
global x1
set(handles.Thumb, 'String', num2str(x1(1)));
drawnow();
You need to call this update routine each time you want the current value of x1 to be updated. It is not possible to have MATLAB automatically call this routine each time it detects that x1 has been changed.
QY Goh
2012 年 5 月 21 日
if i need to always detect the change of x1?that mean i need to always compare it?but my gui is not a figure it is a edit text box isn't possible to use it as well??thank you very much.
Image Analyst
2012 年 5 月 21 日
You know when you change x1. If you don't just search for "x1=" or "x1 =". Then, if you want to change the edit text (or a static text for that matter), just use the set() function. It is not necessary - you only need to do it when you want to update the GUI. For example, in a small loop you might not update it every loop iteration and intead just update it when the loop is done. In a loop, put a "drawnow" after it to force it to update immediately, otherwise it won't get updated right away.
QY Goh
2012 年 5 月 21 日
Hello image analyst,
when i put the drawnow() inside the loop it only able to update latest data to the editbox in my GUI. But the problem still remain. Only the latest data has been updated. And i wish to update it in real time. It ok i post my coding as ur guy reference? May be got some logical error in my coding that i didn't realize about it.Thank You very much.
Walter Roberson
2012 年 5 月 21 日
Posting the code is fine.
There is no way to tell MATLAB to automatically update the GUI when your variable is set: you have to find the places that the variable is set and put in code to update the GUI.
QY Goh
2012 年 5 月 21 日
The Green colour tracking function coding:
function greencolortrack ()
global x1
x1=[0 0 0 0 0];
a = imaqhwinfo;
[camera_name, camera_id, format] = getCameraInfo(a);
count=1;
% Capture the video frames using the videoinput function
% You have to replace the resolution & your installed adaptor name.
vid = videoinput(camera_name, camera_id, format);
% Set the properties of the video object
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 5;
%start the video aquisition here
start(vid)
% Set a loop that stop after 100 frames of aquisition
while(vid.FramesAcquired<=30)
% Get the snapshot of the current frame
data = getsnapshot(vid);
% Now to track red objects in real time
% we have to subtract the red component
% from the grayscale image to extract the red components in the image.
diff_im = imsubtract(data(:,:,2), rgb2gray(data));
%Use a median filter to filter out noise
diff_im = medfilt2(diff_im, [3 3]);
% Convert the resulting grayscale image into a binary image.
diff_im = im2bw(diff_im,0.04);
% Remove all those pixels less than 300px
diff_im = bwareaopen(diff_im,300);
% Label all the connected components in the image.
bw = bwlabel(diff_im, 8);
% Here we do the image blob analysis.
% We get a set of properties for each labeled region.
stats = regionprops(bw, 'BoundingBox', 'Centroid');
% Display the image
imshow(data)
hold on
%This is a loop to bound the red objects in a rectangular box.
for object =1:length(stats)
if object<6
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2)))));
%disp(' X-Coordinate Y-cordinate')
%x=gallery('uniformdata',[5 3],0);
%disp(x)
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
x1(count)=bc(1);
x2 = bc(1);
y1(count)=bc(2);
sort(x1,'ascend');
sort(y1,'ascend');
drawnow update
fprintf('count is %d \n', count);
count=count+1;
if count == 6
count = 1;
end
end
end
hold off
end
%fprintf('%.2f',x1(1));
% Both the loops end here.
% Stop the video aquisition.
stop(vid);
% Flush all the image data stored in the memory buffer.
flushdata(vid);
=================================================================================
This is the coding for GUI
=================================================================================
function varargout = greencolorgui(varargin)
%global thumbvalue
% GREENCOLORGUI M-file for greencolorgui.fig
% GREENCOLORGUI, by itself, creates a new GREENCOLORGUI or raises the existing
% singleton*.
%
% H = GREENCOLORGUI returns the handle to a new GREENCOLORGUI or the handle to
% the existing singleton*.
%
% GREENCOLORGUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in GREENCOLORGUI.M with the given input arguments.
%
% GREENCOLORGUI('Property','Value',...) creates a new GREENCOLORGUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before greencolorgui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to greencolorgui_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 greencolorgui
% Last Modified by GUIDE v2.5 18-May-2012 23:20:52
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @greencolorgui_OpeningFcn, ...
'gui_OutputFcn', @greencolorgui_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 greencolorgui is made visible.
function greencolorgui_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 greencolorgui (see VARARGIN)
% Choose default command line output for greencolorgui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes greencolorgui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = greencolorgui_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 button press in pushbutton1.
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)
%[thumbvalue]=greencolortrack ();
greencolortrack()
global x1
set(handles.Thumb,'string',num2str(x1(1)));
function Thumb_Callback(hObject, eventdata, handles)
% hObject handle to Thumb (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 Thumb as text
% str2double(get(hObject,'String')) returns contents of Thumb as a double
% --- Executes during object creation, after setting all properties.
function Thumb_CreateFcn(hObject, eventdata, handles)
% hObject handle to Thumb (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
function Indexfinger_Callback(hObject, eventdata, handles)
% hObject handle to Indexfinger (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 Indexfinger as text
% str2double(get(hObject,'String')) returns contents of Indexfinger as a double
% --- Executes during object creation, after setting all properties.
function Indexfinger_CreateFcn(hObject, eventdata, handles)
% hObject handle to Indexfinger (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
function middlefinger_Callback(hObject, eventdata, handles)
% hObject handle to middlefinger (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 middlefinger as text
% str2double(get(hObject,'String')) returns contents of middlefinger as a double
% --- Executes during object creation, after setting all properties.
function middlefinger_CreateFcn(hObject, eventdata, handles)
% hObject handle to middlefinger (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
function ringfinger_Callback(hObject, eventdata, handles)
% hObject handle to ringfinger (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 ringfinger as text
% str2double(get(hObject,'String')) returns contents of ringfinger as a double
% --- Executes during object creation, after setting all properties.
function ringfinger_CreateFcn(hObject, eventdata, handles)
% hObject handle to ringfinger (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
function littlefinger_Callback(hObject, eventdata, handles)
% hObject handle to littlefinger (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 littlefinger as text
% str2double(get(hObject,'String')) returns contents of littlefinger as a double
% --- Executes during object creation, after setting all properties.
function littlefinger_CreateFcn(hObject, eventdata, handles)
% hObject handle to littlefinger (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
% --- Executes during object creation, after setting all properties.
function text2_CreateFcn(hObject, eventdata, handles)
% hObject handle to text2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
=================================================================================
The problem was i wish to always updated the green colour tracking value in The Green colour tracking function to the Edit Text In GUI platform in real time.Got Anyway i can do it?Thank You Again~Mr.Walter .
Walter Roberson
2012 年 5 月 21 日
Call the update function after x1=[0 0 0 0 0];
Call the update function after x1(count)=bc(1);
Please be aware that your statement
sort(x1,'ascend');
sorts the content of x1, and then throws away the result of the sorting. You may wish to assign the output of the sort() to something. If you assign to x1, be sure to call the update function right after you do the assignment.
参考
カテゴリ
Help Center および File Exchange で Background Processing についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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 (한국어)