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

5 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Chad Greene
Chad Greene il 6 Nov 2012
Modificato: Chad Greene il 6 Nov 2012
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 Commenti
Chad Greene
Chad Greene il 8 Nov 2012
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
Emerson De Souza
Emerson De Souza il 8 Nov 2012
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

Accedi per commentare.

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by