textscan replaced with textread
조회 수: 9 (최근 30일)
이전 댓글 표시
i have this code. in this i replaced textread with textscan and run now it's showing the errors as index exceeds matrix dimensions .
v=input('Enter No. of Vehicles:');
n=input('Enter No. of Nodes:');
%{
have to change textread function for below function inorder to call the
file from user
%}
fid=fopen('kingsvillestoreslocation.txt')
a=textscan(fid,'txt');
%% Miscellaneous Variables
% Use some otherway of obtaining output
% V=zeros(v);% Output Copy
Y=zeros(n+1);% Distance Matrix
D=zeros(n+1);% Distance Matrix Copy
%% forming distance matrix
for j=1:(n+1)
for i=1:(n+1)
Y(i,j)=sqrt((a(j,1)-a(i,1))^2+(a(j,2)-a(i,2))^2)*69;
end
end
error message is
Enter No. of Vehicles:3
Enter No. of Nodes:6
fid =
11
Index exceeds matrix dimensions.
Error in Untitled (line 26)
Y(i,j)=sqrt((a(j,1)-a(i,1))^2+(a(j,2)-a(i,2))^2)*69;
can anyone help me in this.
thank you in advance.
댓글 수: 0
답변 (2개)
Star Strider
2019년 3월 3일
From the documentation for textscan:
So in all likelihood, ‘a’ is a (1 x 1) cell array. While you can certainly use parentheses to index into a cell array, if the indices are greater than (1,1) (as I believe likely applies here), the code will throw that error.
One solution is to use the cell2mat function to extract the cell array data to a double array. It would be best to change the name the cell array ‘a’ that results from your textscan call, and then use ‘a’ to refer to the matrix extracted from it.
댓글 수: 8
Star Strider
2019년 3월 3일
‘Unable to read file 'kingsvillestorelocation.txt'. No such file or directory.’
You need to put it on your MATLAB search path (see the documentation section on What Is the MATLAB Search Path? (link)), or provide the full path to it. The fullfile (link) function makes this easier, however you still need to provide the necessary information.
Walter Roberson
2019년 3월 3일
you are passing the format 'txt' to textscan . When you pass literal character to textscan then it matches the characters in the input stream and discards them without returning anything . Therefore the a returned will be empty . textscan only returns values for each % entry in the format and only for the ones not followed by * such as %*f meaning to look for a number and discard it.
댓글 수: 2
Walter Roberson
2019년 3월 3일
Look again at https://www.mathworks.com/help/matlab/ref/textscan.html#btghhyz-1-formatSpec and observe that each one of the possibilities is % followed by something. Observe that there is no %t and no %x
Notice too that there is no named option 'txt' -- and if there were then it would have to be followed by a value.
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import and Export에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!