How do you slice in mat lab when its a text file

fid= fopen('cities.txt','r');
City=input('What city are interested in?','s');
filedata=textscan(fid,'%s%s%s%s%s','Delimiter','\t','headerlines',1);
P=find(strcmp(filedata{3},City));
fprintf('%d',P);
lat=filedata{1}(P);
cell2mat(lat);
int(lat(1:2));

7 Comments

What's your goal, exactly? It looks like you want to subset lat which is a element of filedata. But what is lat?
The lat is for the latitude of the cities
I guessed that lat contains latitudes but is it a vector? scalar? And how would you like to subsection it? If Walter's answer doesn't address your query, you'll need to provide more info including the values of relevant variables and what you'd like to do with them.
When extracted from textscan(), for numeric formats such as %f then with the {} selection out of the textscan output, you would be working with a numeric column vector. With a %s format such as was used, the {} selection out of the textscan output is going to give you a cell array of character vectors. The change I proposed is to move from %s to %f for that column so that lat=filedata{1}(P) would be numeric column vector.
The main function of this code is to take the information in the text file and give the user the climate for the city they are interested
that problem has been fixed now my problem is the int(lab(1:2)) part

int() is part of the Symbolic Toolbox, and requests integration. It is not valid to request to integrate a double precision number. It could be valid to request

int(sym(lab(1:2))

but the result would not be very interesting: it would just be

lab(1:2) * x

where here x represents a symbolic variable.

It seems unlikely that you are wanting to do symbolic integration or even numeric integration on latitude data.

Are you perhaps wanting to see only the integer portion of the latitude information? If so then use one of

floor(-45.92)  -> -46
ceil(-45.92)   -> -45
round(-45.92)  -> -46
fix(-45.92)    -> -45

Sign in to comment.

Answers (1)

Change the first %s to %f and remove the last two lines of your code.

Categories

Asked:

on 29 Sep 2018

Commented:

on 1 Oct 2018

Community Treasure Hunt

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

Start Hunting!