How to insert row by row data to excel?

2 views (last 30 days)
I am using five edit text boxes so, the data entered in the text boxes should go to excel every time by pressing the push button the code iam using is
fid=fopen('in.csv','w')
global a1
a1=get(handles.edit1,'string');
b1=str2double(a1);
disp(b1)
global a2
a2=get(handles.edit2,'string');
b2=str2double(a2);
disp(b2)
global a3
a3=get(handles.edit3,'string');
b3=str2double(a3);
disp(b3)
global a4
a4=get(handles.edit4,'string');
b4=str2double(a4);
disp(b4)
global a5
a5=get(handles.edit5,'string');
b5=str2double(a5);
disp(b5)
global a6
a6=get(handles.edit6,'string');
b6=str2double(a6);
disp(b6)
A=[b1,b2,b3,b4,b5,b6];
fprintf(fid,'%f',A(:,:));
fprintf(fid,'\r \n');
fclose(fid);

Answers (1)

Geoff Hayes
Geoff Hayes on 15 Feb 2015
Kasi - it appears that you are just writing your data to a comma-separated file and not an Excel file. Is this intentional? If the idea is to just write out a new row to your file every time that the user presses a button in your GUI, and so you don't want to overwrite the data that you had there already, then you should open your file for appending as
fid=fopen('in.csv','a+')
Declaring variables as global is unnecessary (unless you need to access this data elsewhere). When it comes to writing the data to file, just do
A=[b1,b2,b3,b4,b5,b6];
fprintf(fid,'%f\t',A(:,:));
fprintf(fid,'\n');
fclose(fid);
We use the \t to separate each column of data with a tab. Try the above and see if it does what you expect.

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!