Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB: Write string into Excel

I am trying to write an array of coefficients into an Excel file under 1 column (A1). Here is my snippet:

    filename = 'cheby2coefs.xlsx';

for Order = 1:1 
    fprintf('This is');
    disp(Order);

    [b,a] = cheby2(Order, 20, 300/500);
    disp([b,a]);
    %xlswrite(filename,{'Order ',Order},'A1'); %string
    xlswrite(filename,'b','A1');  %string
    xlswrite(filename,b');
    xlswrite(filename,'a');       %string
    xlswrite(filename,a');
end

The output that i aim for is something like this: enter image description here

However, my code just keeps creating other sheets. What is the proper way to implement this?

like image 320
Erasmus Avatar asked Jan 17 '13 12:01

Erasmus


2 Answers

The problem is in the xlswrite lines:

From xlswrite documentation:

xlswrite(filename,A,sheet) writes to the specified worksheet.

That means you are writing string 'b' to sheet 'A1' with xlswrite(filename,'b','A1');

xlswrite(filename,A) writes array A to the first worksheet in Excel file, filename, starting at cell A1.

You actually do not need to do anything to start writing at A1. Assuming b is a row vector:

xlswrite(filename,b');

as you have written, should suffice.

If you want to specify sheet and column you can use

xlswrite(filename,A,sheet,xlRange)

Update: I cannot try this right now but I think it should work.

You can calculate a and b for every order and write them to an xls file like this:

r = 1; % row number
str = {'a', 'b'};
order = [1 3 5]; % The orders you want to calculate a and b with
for idx = 1:size(order, 2)
    [b,a] = cheby2(order(idx), 20, 300/500); % I do not know about second 
                                             % and third parameters, you should 
                                             % check them.
    vals = [a; b]; % assuming they are row vectors
    cellName = strcat('A', r);
    orderName = strcat('Order ', order(idx));
    xlswrite(filename, orderName, 1, cellName)
    r = r + 1;
    for jdx=1:2
        cellName = strcat('A', r);
        xlswrite(filename, str{jdx}, 1, cellName);
        r = r + 1;
        cellName = strcat('A', r);
        xlswrite(filename, vals(jdx, :), 1, cellName);
        r = r + size(vals, 2);
    end
end
like image 156
HebeleHododo Avatar answered Oct 05 '22 09:10

HebeleHododo


xlswrite keeps creating new sheets with every call because you didn't specify the worksheet number.

Instead, try this:

%// Write b under column A1 in the 1st worksheet
xlswrite(filename, 'b', 1, 'A1')
xlswrite(filename, b(:), 1, 'A2')

It's good practice to make sure that b is written as a column by converting it to a column vector first with the b(:) syntax.

In addition, if you want to write the next set of coefficients under column B1, you can do the following:

%// Write a under column B1 in the 1st worksheet
xlswrite(filename, 'a', 1, 'B1')
xlswrite(filename, a(:), 1, 'B2')

Note that the passed sheet number remains 1.

like image 39
Eitan T Avatar answered Oct 05 '22 09:10

Eitan T