Display data from instrument on a table GUI

11 次查看(过去 30 天)
Hello guys, I need some help with GUI and related stuff. I'd like to create a GUI where there are basically:
  • 1 pop-up menu
  • 2 push button
  • 1 table
The idea is to conncect an instrument in read mode only, select from the pop-up how many repeated measurements the user wants to acquire (4,6 or 8) and consequently add the same number of columns to the table. Then, with the instrument in an "infinite" loop, the user continues to store the data with the table adding row after row until a Save&Exit push button is pressed. This would cause the data to be saved as common .csv file in a folder.
At the moment I have several problems:
  • I'm not able to exit an hypotetical infinite loop even with the Save&Exit push button
  • Thinking to select 4 from the pop-up, I'm not able to populate even the first row of my table (see commented while loop in StartButton_Callback)
In the following code I removed lots of defalut comments made by GUIDE and add a note where I put my personal piece of code.
function varargout = myTest(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @myTest_OpeningFcn, ...
'gui_OutputFcn', @myTest_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 myTest is made visible.
function myTest_OpeningFcn(hObject, eventdata, handles, varargin)
% Format compact and Clear the Command Window
format compact % my code
clc % my code
% Choose default command line output for myTest
handles.output = hObject;
% Initialize the table for storing tha data
set(handles.uitable1,'Data',cell(1,1)) % my code
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes myTest wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = myTest_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;
% connect instrument - mycode
global obj1 % define global variable
% Find a serial port object.
obj1 = instrfind('Type', 'serial', 'Port', '/dev/tty.usbserial-A907OQM5', 'Tag', '') % my instrument
% Create the serial port object if it does not exist
% otherwise use the object that was found.
if isempty(obj1)
obj1 = serial('/dev/tty.usbserial-A907OQM5');
else
fclose(obj1);
obj1 = obj1(1);
end
% Connect to instrument object, obj1.
fopen(obj1);
% Configure instrument object, obj1.
set(obj1, 'FlowControl', 'software');
set(obj1, 'StopBits', 2.0);
set(obj1, 'Terminator', {'CR','LF'});
set(obj1, 'Timeout', 10.0);
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
contents = cellstr(get(hObject,'String'));
a = str2num(contents{2,1});
b = str2num(contents{3,1});
c = str2num(contents{4,1});
switch get(handles.popupmenu1,'Value')
case 2
set(handles.uitable1,'Data',cell(1,a))
case 3
set(handles.uitable1,'Data',cell(1,b))
case 4
set(handles.uitable1,'Data',cell(1,c))
otherwise
end
set(handles.uitable1,'ColumnEditable',true)
guidata(hObject,handles);
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in saveButton.
function saveButton_Callback(hObject, eventdata, handles)
stopValue = get(handles.saveButton, 'Value');
global obj1
% Save data into .txt file - NOT WORKING AT THE MOMENT - work in progress
% C = get(handles.uitable1,'Data');
% T = cell2table(C);
%
% writetable(T,'myTestData.dat','Delimiter','\t','WriteRowNames',true);
%
% type myTestData.dat
% uiputfile('myTestData.dat'); % Type in name of file.
% Close the instrument object
fclose(obj1)
% Close the GUI figure
delete(handles.figure1);
% --- Executes on button press in startButton.
function startButton_Callback(hObject, eventdata, handles)
global obj1
data = get(handles.uitable1,'Data')
i = 1; % counter initialization
% while i <= size(data,2) % bad while loop that doesn't work. Don't know why
data{1,i} = fscanf(obj1, '%s');
set(handles.uitable1,'Data',data);
guidata(hObject,handles);
i = i + 1;
% end
% First idea but doesn't work
% breaks infinte acquisition loop - not working at all - don't know why
% stopValue = get(handles.saveButton, 'Value')
% while true
% fscanf(obj1,'%s')
% stopValue = get(handles.saveButton, 'Value'); %even if I press the save button, its value stays zero --> why!!?!?!
% if stopValue
% break;
% end
% end
Hope someone can give me some tips to perform this task! (why has to be so difficult XD ?!)
Thank you very much for your help.
  1 个评论
vik
vik 2018-12-29
Can you upload both "myTest.m" and "myTest.fig"? GUIs made with GUIDE always consist of two files

请先登录,再进行评论。

回答(1 个)

mz123
mz123 2019-1-3
Dear Vik, thank you for your reply. Please find attached the .fig file.

类别

Help CenterFile Exchange 中查找有关 Instrument Control Toolbox Supported Hardware 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by