how to use delimiter and for loop together to form a matrix in matlab?

5 views (last 30 days)
i have ten ascii files containing 250*2 data and there is some text related to that data. i want to import all files and create 250*10 matrix using all files. i think i can use for loop to import all files and inside loop use of delimiter can remove text from each file. Is there anyone who can help me for coding of this.

Answers (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 19 May 2019
Yes, it is feasible and relatively easy only if all of your files - ASCII formatted data files are well and identically formatted in terms of number of text lines and data lines/columns. If you post one of your data files, that would be very helpful to give you right hints and even provide a code.
  1 Comment
Vishal Dwivedi
Vishal Dwivedi on 19 May 2019
yes of course all files are identically formatted .
say:
1 121
2 122
3 123
.........
.........
250 370
**text**
**text**

Sign in to comment.


Sulaymon Eshkabilov
Sulaymon Eshkabilov on 19 May 2019
Hi,
Here is the solutuion script:
clear variables
N_sets = 10;
for ii = 1:N_sets
Name = strcat(int2str(ii), '.txt'); % DATA files called: 1.txt, 2.text, etc.
FID = fopen(Name, 'r+');
DATA = textscan(FID, '%d %d');
A(:,ii) = DATA{1,1}; % ALL data from Column 1
B(:,ii) = DATA{1,2}; % ALL data from Column 2
% AB = [A,B]; % Augmented data if necessary
fclose(FID);
end
% Note that all texts are ignored/removed as stated in your problem.
Good luck.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!