How to solve a subscript problem by creating a matrix from x,y-arrays?

1 view (last 30 days)
Hi, I use the script below to create two x,y-arrays and to build a matrix with the transposed y-columns of the arrays. Each array correspond to a row of the matrix. (I create, save and import again only to simulate a real situation because I have i-arrays of collected data)
% CREATE ARBITRARY X,Y-ARRAYS:
X1=(3:1:5)';
Y1=rand(1,length(X1))';
M1=[X1 Y1];
X2=(1:1:6)';
Y2=rand(1,length(X2))';
M2=[X2 Y2];
% SAVE I.txt AND IMPORT NEXT TO SIMULATE THE REAL PROBLEM
fid = fopen('1.txt', 'wt');
fprintf(fid, [repmat('%g\t', 1, size(M1,2)-1) '%g\n'], M1.');
fclose(fid);
fid = fopen('2.txt', 'wt');
fprintf(fid, [repmat('%g\t', 1, size(M2,2)-1) '%g\n'], M2.');
fclose(fid);
% CONSTRUCT MATRIX
i=2; % Number of rows (=NUMBER OF FILES)
j=10; % Number of columns (=MIN AND MAX RANGE OF X FOUND IN ALL i.txt FILES)
M = NaN(i,j); % preallocates matrix for speed
for filenumber = 1:i;
fid =fopen(['C:\Users\Emerson\Desktop\',num2str(filenumber),'.txt']);
A=textscan(fid,'%f %f');
fclose(fid);
A = cell2mat(A);
M(filenumber,A(:,1)) = A(:,2);
end
The script works well for entire increment (in the case above 1). The cells m13, m14 and m15 have a value following the range of 1.txt.
In the same way the values of the second row depends on the new range of 2.txt. One can change the ranges of 1.txt (=M1) and 2.txt(=M2) by varying X1 and X2 above and the matrix will adjust accordingly.
Problem: My real i.txt arrays have increments of 0.5 and if I change the increment above I obtain the following error:
Subscript indices must either be real positive integers or logicals.
I wonder if someone could help me to fix this.
Thank you in advance for your help.
Emerson
  1 Comment
Arthur
Arthur on 12 Nov 2012
At which line does the error occur? Please paste the entire error message.

Sign in to comment.

Accepted Answer

bym
bym on 9 Nov 2012
perhaps something like this
for filenumber = 10:5:i*10;
fid =fopen(['C:\Users\Emerson\Desktop\',num2str(filenumber/10),'.txt']);
...
end
or perhaps using the dir command
files = dir('C:\Users\Emerson\Desktop\*.txt');
for filenumber = 1: length(files)
...
end
  1 Comment
Emerson De Souza
Emerson De Souza on 11 Nov 2012
Hi proecsm, unfortunately these suggestions do not solve my problem.
Thanks anyways
Emerson

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by