Save data in excel sheet using pushbutton.

2 views (last 30 days)
sidra Rafique
sidra Rafique on 14 Apr 2019
Commented: sidra Rafique on 14 Apr 2019
i am using below code at the click of pushbutton. i want to create an excel sheet. which has 2 colums(Frames(1x1 double) & Diameter(1x1 double)) and multiple rows. for this code it will write only 1 row.
% --- Executes on button press in SaveTag.
function SaveTag_Callback(hObject, eventdata, handles)
% hObject handle to SaveTag (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% global Whole_Data
Whole_Data = {'Frames','Diameter'};
framecount=getappdata(handles.UFramesTag,'original');
dia=getappdata(handles.CalculateTag,'Diam');
Data = {framecount, num2str(dia)};
Whole_Dataa = vertcat(Whole_Data, Data);
xlswrite('Diameters.xlsx', Whole_Dataa);
but this is thowing an error. and i dont know what dimensions i have to change to make it work. Error screenshot is below.

Answers (1)

Guillaume
Guillaume on 14 Apr 2019
The problem is either with framecount or dia. We have no idea what size they are nor what their type is. It's also unclear why you convert dia into a string. Why not write the number directly in excel?
You would probably find it much easier to use modern matlab tools such as writetable instead of xlsread. Your code may be equivalent (no guarantee since as said, we don't know anything about framecount and dia):
framecount=getappdata(handles.UFramesTag,'original');
dia=getappdata(handles.CalculateTag,'Diam');
WholeData = table(framecount, dia, 'VariableNames', {'Frames', 'Diameter'});
writetable(WholeData, 'Diameters.xlsx');
  1 Comment
sidra Rafique
sidra Rafique on 14 Apr 2019
framecount has count of each frame. in this code it has frame no 02, dia has a diameter against this frame.i cant tell you about their types. because i dont know how to check their types.,As my workspace is not showing any details getappdata variables.
Secondly, converting dia into string is not necessary, iwas just doing hit and trial to resolve error.Otherwise without converting to string also doesnot create any positive change to error.
Lastly, i have used your above mentioned code(mentioned below). but its still not working and throwing below error.
Please help me with this error.
% --- Executes on button press in SaveTag.
function SaveTag_Callback(hObject, eventdata, handles)
framecount=getappdata(handles.UFramesTag,'original');
dia=getappdata(handles.CalculateTag,'Diam');
% Data = {framecount, dia};
% Whole_Dataa = vertcat(Whole_Data, Data);
% xlswrite('Diameters.xlsx', Whole_Dataa);
% winopen('Diameters.xlsx');
WholeData = table(framecount, dia, true, {'Frames', 'Diameter'});
writetable(WholeData, 'Diameters.xlsx');
Error
>> ImageInterpolation
Error using tabular/verifyCountVars (line 339)
All variables must have the same number of rows.
Error in table (line 276)
numRows = tabular.verifyCountVars(vars);
Error in ImageInterpolation>SaveTag_Callback (line 172)
WholeData = table(framecount, dia, true, {'Frames', 'Diameter'});
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in ImageInterpolation (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)ImageInterpolation('SaveTag_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!