command to merge .txt files

Hello,
I have multiple .txt files. Every .txt files has 1 row and 4 columns. I would like to merge these many files to one .txt file , with vertical format.
I use these commands in my code
txtFiles = dir('*.txt') ; % get the text files in the present folder
N = length(txtFiles) ; % Total number of text files
iwant = cell(N,1) ; % initlaize the data required
% loop for each file
for i = 1:N
thisFile = txtFiles(i).name ;
iwant{i} = importdata(thisFile) ; % read data of the text file
end
iwant = cell2mat(iwant) ;
but command window shows me an error:
Error using importdata (line 10)
Unable to load file.
Use readtable for more complex formats.
Caused by:
Index exceeds the number of array elements (1).
could anyone help me?
I am importing one .txt file in order to understand

8 Kommentare

KSSV
KSSV am 10 Jun. 2020
Attach your .txt file..what data it has? If not importdata, try load.
Rik
Rik am 10 Jun. 2020
Why are all those NULLs in your file?
Ivan Mich
Ivan Mich am 10 Jun. 2020
I do not know. I just wrote this:
for j=1:size(data)
......
fid = fopen(sprintf( 'file%g.txt',j), 'wt' );
fprintf(fid,'%s\t',St{j},R1,b,2)
fclose(fid)
end
in order to create them?
Can I remove the NULLs with one way?
Anyway how could I solve this?
Stephen23
Stephen23 am 10 Jun. 2020
"Can I remove the NULLs with one way?"
Not printing them in the first place would be a much better option.
Ivan Mich
Ivan Mich am 10 Jun. 2020
How could I do that? I do not understand you
Rik
Rik am 10 Jun. 2020
Apparently your data doesn't just contain normal char arrays, there are also NULL characters in there: char(0).
Ivan Mich
Ivan Mich am 10 Jun. 2020
KSSV I have Imported my file1.txt
As Stephen mentioned, you should avoid printing the NULL in the first place. You are probably trying to print numeric values as text values. Try the following line to create the text files
fprintf(fid,'%f\t',St{j},R1,b,2)

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Large Files and Big Data finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 10 Jun. 2020

Kommentiert:

am 11 Jun. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by