How to extract values from an array and write at a specific cell of a matrix?

INTRODUCTION: Hi, I have multiple arrays i.txt made of two columns and n-rows in a folder. The examples below display the first two arrays:
% 1.txt
fid =fopen('C:\Users\EMERSON\Desktop\ARRAYS\1.txt');
A1=textscan(fid,'%f %f');
fclose(fid);
% 3 130
% 4 340
% 5 159
% 6 030
% 7 100
% 2.txt
fid =fopen('C:\Users\EMERSON\Desktop\ARRAYS\2.txt');
A2=textscan(fid,'%f %f');
fclose(fid);
% 10 125
% 11 300
% 12 110
GOAL: I want to construct a matrix M with elements of the second column of i.txt as below:
1 2 3 4 5 6 7 8 9 10 11 12 13
NaN NaN 130 340 159 030 100 NaN NaN NaN NaN NaN NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN 125 300 110 NaN
EXPLANATION:
A value is assigned for each column of M (First row of M above, but it will not appear later, I display here only for clarification).
Then I import the array 1.txt. If the first column of the array 1.txt contains a value assigned in M, then I extract the corresponding value from the second column of 1.txt and write in the cell of the matrix designed for it (first row and j-column).
And I write NaN (not available) in the cells of M for which the value is not found in i.txt.
Then I import the array 2.txt and do the same in the second row of the matrix, and so on for i.txt arrays. The resulting matrix will contain i-rows and j-columns, where j is the range of values pre-assigned.
I wonder if someone could help me how to write the commands that does this extraction and write in the proper place of the matrix.
Thank you in advance for your help
Emerson

 Accepted Answer

Emerson,
Try this, and replace numberoffiles with how many .txt files you have. Also if you have more than 13 columns in you M matrix, change numberofcolumns accordingly:
numberoffiles=2;
numberofcolumns=13;
M = NaN(numberoffiles,numberofcolumns); % preallocates matrix for speed
for filenumber = 1:numberoffiles
fid =fopen(['C:\Users\EMERSON\Desktop\ARRAYS\',num2str(numberoffiles),'.txt']);
A=textscan(fid,'%f %f');
fclose(fid);
A = cell2mat(A);
M(filenumber,A(:,1)) = A(:,2);
end
M

3 Comments

Hi Chad,
thanks for your help. I tried your suggestion and M looks like below:
NaN NaN NaN NaN NaN NaN NaN NaN NaN 125 300 110 NaN
NaN NaN NaN NaN NaN NaN NaN NaN NaN 125 300 110 NaN
Do you know what is going on? It seems that the first row of M is overwritten by the second row.
VERY IMPORTANT: The values of the first column of i.txt are not necessarily always entire numbers, they can be also fractional such as 1.3, 2.5 and so on.
THEREFORE, we need:
(1) a command that gives the increment for the columns and assign the values for each column such as (j=0:0.5:10);
(2) a command that search in the first column of i.txt the "MATCHING VALUES ASSIGNED FOR EACH COLUMN OF M" and writes the corresponding value of the second column of i.txt
In principle, you got my idea, but we can't use the column numbers as the searching matching value assigned for each j-cell.
I hope you know what I meant, thanks in advance for your support
Emerson
Emerson,
My mistake! Change
fid =fopen(['C:\Users\EMERSON\Desktop\ARRAYS\',num2str(numberoffiles),'.txt']);
to
fid =fopen(['C:\Users\EMERSON\Desktop\ARRAYS\',num2str(filenumber),'.txt']);
As for the other part of your question, I suspect the best solution depends on what you're trying to do. If you want to keep the two columns of data attached to their respective file numbers, that's three dimensions of data, so perhaps you need a three-dimensional matrix. If you want a simple column with all of your x data, and another column with all your y data, you could create x and y variables, then make a loop to open .txt files and append the x and y variables with each iteration of the loop (tack values from filenumber.txt on to the bottom of your x and y columns). If, at the end you want all that data to be sorted, you can sort the columns accordingly.
Hope this helps, Chad
Hi Chad, thanks again for your help.
You gave a great suggestion and my problem is partially solved.
I will formulate the specific problems in a new question and address them with more clear example.
Wish you a nice evening
Emerson

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!