1 year ago

#76268

test-img

Oiko

Append data to file in new line in Matlab using fwrite

I'm using the following code to select a bunch of csv files, load each one and append to a new csv:

[filenames, folder] = uigetfile('*.csv','Select the data file','MultiSelect','on')   

% Create output file name in the same folder.
outputFileName = fullfile(folder, 'new_file.csv')     % NAME THE NEW FILE
fidOutput = fopen(outputFileName, 'wt');          
    for k = 1 : length(filenames)
    % Get this file name.
    thisFileName = fullfile(folder, filenames{k})
    % Open input file:
    fidInput = fopen(thisFileName);  
    % Read text from it
    thisText = fread(fidInput, '*char');
    % Copy to output file:
    fwrite(fidOutput, thisText);    
    fclose(fidInput);   % close the input file
end
fido=fclose(fidOutput);

However, when I inspect the new file, the first line of each file is appended in front of the last line of the previous file. See image for example with 3 files containing 5 lines each.

exmaple

I need everything to be aligned in order to load each column in a subsequent step.

fwrite does not seem to have a \n input to create a line break. Is there a way to correct this? I'm on matlab 2015b

matlab

csv

0 Answers

Your Answer

Accepted video resources