Add a new row in an uitable dynamically using a pushbutton

8 views (last 30 days)
Hello everybody
I'm creating an app for making electrical calculations but I have to add rows constantly for entrying new data to be evaluated. I'm using a code i saw on a MatLAB tutorial on Youtube but it doesn't run as I was expected. I would like some help to improve this code and make it works properly.
The code I'm using is:
% --- 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)
data=get(handles.uitable1, 'data');
data=str2double(data);
data(end+1,:)=0;
set(handles.uitable1, 'data', data);
I appreciate for your help.

Answers (2)

Joseph Cheng
Joseph Cheng on 13 Aug 2014
Edited: Joseph Cheng on 13 Aug 2014
what type of stuff are you sticking in this table? What are you expecting? if the data variable (place a breakpoint at data and see) is a string of numbers? letters?
is data at the get(handles.uitable1,'data') a cell array? if so i wouldn't convert data from a str2double as it could mess up your data depending on whats there. i would just go
data=get(handles.uitable1, 'data');
data(end+1,:)={0}; %if data is a cell or
data(end+1,:)=0; %if data is an array.
set(handles.uitable1, 'data', data);
  4 Comments
Joseph Cheng
Joseph Cheng on 19 Aug 2014
Has by answers resolved your initial question? if so can you mark it as answered?
To answer your question below i would just convert the cell type into a mat then do the str to number conversion or vice versa. I don't have a sample of your cases so you'll have to check out the different functions and how to perform the conversion.

Sign in to comment.


Julian
Julian on 13 Aug 2014
Edited: Julian on 13 Aug 2014
function varargout = AddRow(varargin)
% ADDROW MATLAB code for AddRow.fig
% ADDROW, by itself, creates a new ADDROW or raises the existing
% singleton*.
%
% H = ADDROW returns the handle to a new ADDROW or the handle to
% the existing singleton*.
%
% ADDROW('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in ADDROW.M with the given input arguments.
%
% ADDROW('Property','Value',...) creates a new ADDROW or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before AddRow_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to AddRow_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 AddRow
% Last Modified by GUIDE v2.5 13-Aug-2014 17:26:00
% Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @AddRow_OpeningFcn, ... 'gui_OutputFcn', @AddRow_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 AddRow is made visible. function AddRow_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 AddRow (see VARARGIN)
% Choose default command line output for AddRow handles.output = hObject;
% Update handles structure guidata(hObject, handles);
% UIWAIT makes AddRow wait for user response (see UIRESUME) % uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line. function varargout = AddRow_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)
data=get(handles.uitable1, 'data'); % Get data as a string
data(end+1,:)={0}; % Add new row of 0's as a cell type
set(handles.uitable1, 'data', data); % I have a new uitable with a rows with string type and other rows as cell type
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future ve
% handles structure with handles and user data (see GUIDATA)
data=get(handles.uitable1, 'data'); % Gets the actual data for use
data=str2double(data); % Change all strings inside the uitable to a double type
X=data(:,1); % Sets the X vector from the data
Y=data(:,2); % Sets the Y vector from the data
axes(handles.axes1) % Sets the axes for use
plot(X,Y) % Plot X vs. Y in the selected axes.
How can I use the data if I have an string type and a cell type into the uitable?
  2 Comments
imene. B
imene. B on 11 Jul 2016
Edited: imene. B on 11 Jul 2016
thank you very much julian :D
here's my code, it works perfectly
Add a row
% --- 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)
data=get(handles.uitable1, 'data');
data(end+1,:)={0};
set(handles.uitable1, 'data', data);

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!