ALI
ALI

Reputation: 339

how to Save Data in Excel Sheet using Matlab?

I want to save my data in form of table in Excel Sheet. It should look like:

Name |  Age |   R_no |  Gpa
Adnan |     24 |    18 |    3.55  
Ahmad |     22 |    12 |    3.44
Usman |     23 |    22 |    3.00

Every time when I will execute my file classData.m , A row will be added below. like I want to add next row as

john | 21 | 44 | 3.53

variable n='john', ag=22, rn=44, gp=3.53

Upvotes: 2

Views: 10158

Answers (3)

Adnan Ali
Adnan Ali

Reputation: 3055

Using code given by @Tom

It will create a new file if it doesn't already exist, and if it does exist, then it will append the row below.

filename='Features.xlsx';
N='Adnan'; a=22; roll=22; gpa=3.55;
fileExist = exist(filename,'file'); 
if fileExist==0
    header = {'Name', 'age ','roll' , 'gpa'};
    xlswrite(filename,header);


else

    [~,~,input] = xlsread(filename); % Read in your xls file to a cell array (input)
    new_data = {N, a,roll , gpa}; % This is a cell array of the new line you want to add
    output = cat(1,input,new_data); % Concatinate your new data to the bottom of input
    xlswrite(filename,output); % Write to the new excel file. 

end

Upvotes: 2

Tom
Tom

Reputation: 142

Make your table as a cell matrix then use xlswrite to save it to xls

That will allow you to export a table with mixed content types (numbers and text).

[~,~,input] = xlsread('your_file.xls') % Read in your xls file to a cell array (input)
new_data = {'john', 22, 44, 3.53} % This is a cell array of the new line you want to add
output = cat(1,input,new_data); % Concatinate your new data to the bottom of input
xlswrite('output_file_name.xls',output); % Write to the new excel file. 

If you want to keep the same file name then you can change this slightly so its like this

file = 'file_to_update.xls';
[~,~,input] = xlsread(file) % Read in your xls file to a cell array (input)
new_data = {'john', 22, 44, 3.53} % This is a cell array of the new line you want to add
output = cat(1,input,new_data); % Concatinate your new data to the bottom of input
xlswrite('file',output); % Write to the new excel file. 

Is that more helpful?

Upvotes: 1

Nematollah Zarmehi
Nematollah Zarmehi

Reputation: 694

You can use xlswrite() function as follow:

filename = 'Data.xlsx';
Data = {'john', 22, 44, 3.53};
xlswrite(filename,Data);

Fore more information you can read help for xlswrite() function.

Upvotes: 1

Related Questions