# MATLAB: How to extract values from an array and write at a specific cell of a matrix

cell arraysMATLABmatrix manipulation

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.txtfid =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.txtfid =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.
numberoffiles=2;numberofcolumns=13;M = NaN(numberoffiles,numberofcolumns); % preallocates matrix for speedfor filenumber = 1:numberoffilesfid =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