Hello everyone
İ am trying to display a counter on a GUİ.
Each time i click on a Next button , how is the counter going. The counter info is coming from excel.
İ have already used a static text in the gui and i renamed it exp_counter . İ would like just to display what is happening in the background.
function franck_guide_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 franck_guide (see VARARGIN)
% Choose default command line output for franck_guide
handles.output = hObject;
% Plot patch on uiaxes
%hold on
% Read experiment data from a CSV file
[~,~,data] = xlsread('excel_data_final.xlsx');
data(1,:) = []; % remove the header line
% randomly permute the rows of data without repeating the value:
data = data(randperm(size(data,1)),:);
numeric_data = cell2mat(data(:,[1 2 3 4 6]));
handles.v_thickness_1 = numeric_data(:,1); % numeric
handles.v_thickness_2 = numeric_data(:,2);
handles.h_thickness_1 = numeric_data(:,3);
handles.h_thickness_2 = numeric_data(:,4);
handles.amplitude = data(:,5); % cell array of char vectors
handles.v_or_h_array = numeric_data(:,5);
handles.f_df = data(:,7);
handles.exp_counter = 1;
handles.region1 = [];
% Create the Arduino serial object
handles.arduinoObj = serialport('COM3', 38400);
configureTerminator(handles.arduinoObj,'CR/LF');
%
for i=1:8
handles.message = readline(handles.arduinoObj);
disp(handles.message)
end
create_patch(handles);
function Next_button_Callback(hObject, eventdata, handles)
% hObject handle to Next_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)uiconfirm(handles.UIFigure,'Are You sure?','Confirm Close',...
handles = guidata(hObject);
handles.exp_counter = handles.exp_counter + 1;
if handles.exp_counter > numel(handles.v_thickness_1)
% set(handles.exp_counter);
msgbox('Experiment is Done!','DONE');
return
end
f = msgbox('Operation Completed','NEXT');
% delete the old patch and create a new one:
create_patch(handles);

 Accepted Answer

Voss
Voss on 17 May 2022
Edited: Voss on 17 May 2022
If your static text is called handles.exp_counter, then that's going to be a problem because the name handles.exp_counter already refers to the counter variable (initialized to 1 in the OpeningFcn and incremented by 1 in Next_button_Callback).
Instead, call your static text something else, e.g., handles.text_exp_counter, and then set its String in the OpeningFcn and in Next_button_Callback (i.e., whenever the counter handles.exp_counter changes):
function franck_guide_OpeningFcn(hObject, eventdata, handles, varargin)
% ...
% ...
handles.exp_counter = 1;
set(handles.text_exp_counter,'String',num2str(handles.exp_counter));
% ...
function Next_button_Callback(hObject, eventdata, handles)
% hObject handle to Next_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)uiconfirm(handles.UIFigure,'Are You sure?','Confirm Close',...
handles = guidata(hObject);
new_counter = handles.exp_counter + 1;
if new_counter > numel(handles.v_thickness_1)
msgbox('Experiment is Done!','DONE');
return
end
f = msgbox('Operation Completed','NEXT');
set(handles.text_exp_counter,'String',num2str(new_counter));
handles.exp_counter = new_counter;
% delete the old patch and create a new one:
create_patch(handles); % this calls guidata(), which stores the new handles.exp_counter value
(doing num2str is not strictly necessary)

5 Comments

@_ Thank you . There is a lıttle issue that came up..,
The default line that is displayed at the very beginning seems not taking into consideration even though 1'' is displayed.
When i click on Next , it is still '1' even though the line has changed. After that everything woks fine
Hmm. Please verify that the code in Next_button_Callback is the same as I have it here, because I updated it after initially posting.
If the code is the same and handles.text_exp_counter String doesn't update correctly, you can try putting a drawnow() after setting the String:
set(handles.text_exp_counter,'String',num2str(new_counter));
drawnow();
handles.exp_counter = new_counter;
(but that would only eliminate any delay in updating the String due to create_patch having to run first, and it wouldn't explain why it's ok after the first Next click.)
Check for anywhere in the code handles.exp_counter might be reset to 0, and also verify that guidata(hObject,handles), i.e., guidata with two arguments, is still being called in create_patch (so that the new value of handles.exp_counter is stored).
@_ the drawnow() has fixed it. Thank you very much
Excellent! You're welcome!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!