Waleed
Waleed

Reputation: 59

How to export my data to CSV?

Matlab help: I am trying to export all 50 points to a CSV. How can I append the csv files after each iteration?

 %import csv file

    filename = 'Q:\Electroporation\raw_works.csv';
    delimiter = ',';
    startRow = 2;
    %% Format for each line of text:
    formatSpec = '%s%f%f%f%f%f%f%f%f%f%[^\n\r]';
    %% Open the text file.
    fileID = fopen(filename,'r');
    %% Read columns of data according to the format.
    dataArray = textscan(fileID, formatSpec, 'Delimiter', delimiter, 'TextType', 'string', 'HeaderLines' ,startRow-1, 'ReturnOnError', false, 'EndOfLine', '\r\n');

    %% Close the text file.
    fclose(fileID);
    %% Create output variable
    rawworks = table(dataArray{1:end-1}, 'VariableNames', {'Name','One','Two','Three','Four','Five','Six','Seven','Eight','Nine'});
    %% Clear temporary variables
    clearvars filename delimiter startRow formatSpec fileID dataArray ans;

So far the data in MATLAB.

% store data into a variable
    table= rawworks;
    array=str2double(table2array(table)); % convert to array
    ss= size(array) 
    N= ss(1)
    ones=ones(50,14);
    xls=zeros(50,14);

Now I do the math operation

    for i= 1:N 
        A=[array(i,2) array(i,3) array(i,4);array(i,5) array(i,6) array(i,7);array(i,8) array(i,9) array(i,10)];

       %diognalize
        [U,S,V]=svd(A);
        P1=S(1,1);
        P2=S(2,2);
        P3=S(3,3);

%output data
          data_for_excel_file=[A(1,1) A(1,2) A(1,3) A(2,1) A(2,2) A(2,3) A(3,1) A(3,2) A(3,3) P1 P2 P3 P1/P2 P1/P3 ]

here is where I'm having problems. How can I make csvwrite append to the end of % file. Currently, it is only writing out the last result instead of all 50.

      csvwrite('Diognalized_output.csv',data_for_excel_file,1)  %HELP

    end

Upvotes: 1

Views: 1657

Answers (1)

informaton
informaton

Reputation: 1482

If you are okay using the more general function dlmwrite instead, you can use its -append flag to add your output to the end of the file each time.

Change your last line from

csvwrite('Diognalized_output.csv',data_for_excel_file,1) 

to

 dlmwrite('Diognalized_output.csv,data_for_excel_file,'-append')

The default delimiter for dlmwrite is the comma (,) so you get the same output format here.

Upvotes: 1

Related Questions