hello, i have such a type of data per 30 days in one file.by using fgets,fscanf,str2num,strcmp i need to plot temp vs height
Show older comments

5 Comments
Dyuman Joshi
on 8 Dec 2023
Moved: Dyuman Joshi
on 9 Dec 2023
Mr Thadi
on 8 Dec 2023
Moved: Dyuman Joshi
on 9 Dec 2023
Dyuman Joshi
on 8 Dec 2023
Moved: Dyuman Joshi
on 9 Dec 2023
Any particular reason why you are constrained you use those functions only, when there are better, robust functions available?
Mr Thadi
on 11 Dec 2023
Dyuman Joshi
on 5 Feb 2024
@Mr Thadi, did you check the answer posted below?
Answers (1)
The following uses fscanf, fgets, str2num, and strcmp:
filename = 'file.txt';
fid = fopen(filename,'r');
counter = 0;
while counter < 2
str = fgets(fid);
if ~ischar(str)
break
end
if numel(str) > 1 && strcmp(str([1 2]),'--')
counter = counter + 1;
end
end
data = str2num(fscanf(fid,'%c'));
fclose(fid);
plot(data(:,2),data(:,3))
Categories
Find more on Data Import from MATLAB in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!