textscan replaced with textread

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 个评论

will this code works
fid=fopen('kingsvillestoreslocation.txt')
a=textscan(fid,'%s');
a=cell2mat(a);
fclose(fid)
can you please help "
It will likely produce a column cell array of strings one string long. I can be more helpful if you attach ‘kingsvillestoreslocation.txt’ so I can see what it is and determine how best to read it.
fid = fopen('kingsvillestorelocation.txt');
a = cell2mat(textscan(fid, '%f %f'));
fclose(fid);
Or more simply with that file that has no headers and just numeric data:
a = load('kingsvillestorelocation.txt', '-ascii');
I would go with the load (link) function.
It is simply easier with your file.
when i run this code i am getting error as given
Enter No. of Vehicles:3
Enter No. of Nodes:9
Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in Untitled (line 13)
a = cell2mat(textscan(fid, 'txt'));
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')
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.

请先登录,再进行评论。

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 个评论

txt is format
from my knowledge textscan syntax fid and format are the values i should pass.
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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Matrix Indexing 的更多信息

产品

版本

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by