How to detect the uitable on GUI

4 ビュー (過去 30 日間)
han han
han han 2020 年 7 月 4 日
回答済み: Image Analyst 2020 年 7 月 4 日
I wnat to detect whether the uitable on the GUI is empty.
but it shows "Input #2 expected to be a cell array, was double instead."
If I change the data format to cell, it will not be able to enter "all(cellfun(@isempty, data(:)))"
data = get(handles.uitable1);
if all(cellfun(@isempty, data(:)))
errordlg('uitable is empty','warning');
else
disp('not empty');
end
how modifity it?
  2 件のコメント
Mehmed Saad
Mehmed Saad 2020 年 7 月 4 日
this error is occuring in someother line of code. can you share the complete code?
han han
han han 2020 年 7 月 4 日
編集済み: han han 2020 年 7 月 4 日
sure
function varargout = u11ntitled(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @u11ntitled_OpeningFcn, ...
'gui_OutputFcn', @u11ntitled_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 u11ntitled is made visible.
function u11ntitled_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 u11ntitled (see VARARGIN)
% Choose default command line output for u11ntitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes u11ntitled wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = u11ntitled_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
% --- Executes on button press in next.
function next_Callback(hObject, eventdata, handles)
set(handles.uipanel2,'Visible','on') %go to uipanel1
data = get(handles.uitable1);
if all(cellfun(@isempty, data(:)))
errordlg('uitable is empty','warning');
else
disp('not empty');
end
% --- Executes on button press in back.
function back_Callback(hObject, eventdata, handles)
set(handles.uipanel2,'Visible','off') %back to uipanel1
% --- Executes on button press in show.
function show_Callback(hObject, eventdata, handles)
[BSLocation] = textread('Convert\BSLocation.txt','','headerlines', 1);
BSX = num2str(BSLocation(:,1));
BSX = cellstr(BSX);
BSY = num2str(BSLocation(:,2));
BSY = cellstr(BSY);
BSZ = num2str(BSLocation(:,3));
BSZ = cellstr(BSZ);
set(handles.Uitable1,'Data',[BSX BSY BSZ]);

サインインしてコメントする。

回答 (2 件)

Mehmed Saad
Mehmed Saad 2020 年 7 月 4 日
all(cellfun(@isempty, data(:)))
error is in this line. data(:) is a double array on which you are trying to apply cellfun
also you have uitable1 and Uitable1 which are seperate variables (means two seperate table).
If uitable1 is a table this should be your command
data = get(handles.uitable1,'Data');
and now the data is not a cellarray so cellfun is not required
you can apply isempty directly
For example
t = readtable('patients.xls');
vars = {'Age','Systolic','Diastolic','Smoker'};
t1 = t(1:0,vars);
fig = uifigure;
uit = uitable(fig,'Data',t1);
Now check if it is empty
if(isempty(uit.Data))
t2 = t{1:4,vars};
uit.Data = t2;
end

Image Analyst
Image Analyst 2020 年 7 月 4 日
You can use isempty() to determine if a variable is null.
You can use iscell() to determine if a variable is a cell array.
There is a function called isa() that lets you check whether a variable is of a specified class, like double, uint8, cell, etc.

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by