textscan replaced with textread

조회 수: 7 (최근 30일)
Bhargavkrishna Kondreddy
Bhargavkrishna Kondreddy 2019년 3월 3일
댓글: Star Strider 2019년 3월 3일
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.

답변 (2개)

Star Strider
Star Strider 2019년 3월 3일
From the documentation for textscan:
C = textscan(fileID,formatSpec) reads data from an open text file into a cell array, C.
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
Bhargavkrishna Kondreddy
Bhargavkrishna Kondreddy 2019년 3월 3일
i also used the load
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
%}
a = load('kingsvillestorelocation.txt', '-ascii');
error
Error using load
Unable to read file 'kingsvillestorelocation.txt'. No such file or directory.
Error in Untitled (line 12)
a = load('kingsvillestorelocation.txt', '-ascii')
Star Strider
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
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
Bhargavkrishna Kondreddy
Bhargavkrishna Kondreddy 2019년 3월 3일
txt is format
from my knowledge textscan syntax fid and format are the values i should pass.
Walter Roberson
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 CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by