MATLAB Answers

0

textscan replaced with textread

Latest activity Commented on by Star Strider
on 3 Mar 2019
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 Comments

Sign in to comment.

Products


Release

R2016b

2 Answers

Answer by Star Strider
on 3 Mar 2019

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 Comments

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.

Sign in to comment.


Answer by Walter Roberson
on 3 Mar 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 Comments

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.

Sign in to comment.