How do you slice in mat lab when its a text file
Show older comments
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
Adam Danz
on 30 Sep 2018
What's your goal, exactly? It looks like you want to subset lat which is a element of filedata. But what is lat?
Francisco Michel
on 30 Sep 2018
Adam Danz
on 30 Sep 2018
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.
Walter Roberson
on 30 Sep 2018
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.
Francisco Michel
on 1 Oct 2018
Francisco Michel
on 1 Oct 2018
Walter Roberson
on 1 Oct 2018
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
Answers (1)
Walter Roberson
on 30 Sep 2018
0 votes
Change the first %s to %f and remove the last two lines of your code.
Categories
Find more on Data Import and Export in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!