textscan replaced with textread
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (2)
Star Strider
am 3 Mär. 2019
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 Kommentare
Star Strider
am 3 Mär. 2019
‘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
am 3 Mär. 2019
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 Kommentare
Walter Roberson
am 3 Mär. 2019
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.
Siehe auch
Kategorien
Mehr zu Data Import and Export finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!