How to solve a subscript problem by creating a matrix from x,y-arrays?
조회 수: 3 (최근 30일)
이전 댓글 표시
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
채택된 답변
bym
2012년 11월 9일
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Operators and Elementary Operations에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!