Import Multiple Text Files into multiple arrays

I have 62 flies - each file has 4 columns containing variable number of rows (between 226-300 rows). The number of rows corresponds to the number of raindrops. The first column is the X coordinate of the start of the raindrop, the second column is theh Y coordinate of the start of the raindrop. The last two columns are the X and Y coordinates of the end of the raindrop. The data is decimals
The files all have the same start to the name 'XYpoints_timeFromDeparture_Z.txt' where Z is 0,30,60 etc all the way to 1830
I need to find a way, using a for loop, to import all the data - then each file needs to put the data from cells into arrays.
This is how I did it for one file:
fileName = 'XYpoints_timeFromDeparture_0.txt';
fileID = fopen(fileName);
XXYY = textscan(fileID,'%f %f %f %f'); % puts the data into cells
fclose(fileID);
A = cell2mat(XXYY) % cell to array
we have XXYY which is a 1x4 array. Each cell is a vector (Xa, Ya, Xb, Yb)
What I need to do is make a an array as a 62x1. Each cell needs to have the XXYY which then has the vector per cell.
I tried using this code.
for i = 0:30:1830
fileName ='XYpoints_timeFromDeparture_%d.txt', i;
fileID = fopen(fileName);
XXYY = textscan(fileID,'%f %f %f %f'); % puts the data into cells
fclose(fileID);
A = cell2mat(XXYY) % cell to array
end
Doing this, gets me the error using textscan

3 Kommentare

replace
fileName ='XYpoints_timeFromDeparture_%d.txt', i;
by
fileName =sprintf( 'XYpoints_timeFromDeparture_%d.txt', i );
And notice that you overwritr A in each iteration
how do i prevent that?
Stephen23
Stephen23 am 15 Apr. 2020
"how do i prevent that?"
See the examples in the MATLAB documentation:

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Stephen23
Stephen23 am 15 Apr. 2020
Bearbeitet: Stephen23 am 15 Apr. 2020

0 Stimmen

D = 'path to the directory where the files are saved';
V = 0:30:1830;
N = numel(V);
C = cell(1,N);
for k = 1:N
F = sprintf('XYpoints_timeFromDeparture_%d.txt',V(k));
[fid,msg] = fopen(fullfile(D,F),'rt');
assert(fid>=3,msg)
C(k) = textscan(fid,'%f%f%f%f', 'CollectOutput',true);
fclose(fid);
end
All of the imported data will be in the cell array C:

4 Kommentare

When I use this code, cell array C is a 1x62 (which is great!) but each cell is empty. I need each cell to have an array of a 4x1 and each row to have a vector (which is the column of data from the text file)
Now there's this error
Error using fopen
Invalid permission.
Error in q2 (line 8)
[fid,msg] = fopen(fullfile(D,F),' rt');
Stephen23
Stephen23 am 15 Apr. 2020
Bearbeitet: Stephen23 am 15 Apr. 2020
For some reason you added a space character:
fopen(fullfile(D,F),'rt') % My answer shows this
fopen(fullfile(D,F),' rt') % You tried this
% ^ You added this
Ah yes - it works perfectly now thank you!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by