Grab tempretures higher than 299

2 views (last 30 days)
I have made this program that grabs tempretures from a text file while ignoring the other characters in the cell to plot but i only grab temps from 200-299 and I want to expand
clear all; close all; clc;
%read data from the text file by
%insert your textfile to open
file = fopen('rocks1.txt', 'rt');
%put file data into array
raw = textscan(file,'%s');
txt = raw{1,1};
%extract only numbers from array
temps = regexp(txt, '(\w*2)[\d.]+', 'match');
%regexp only reads 200K to 299K, need to go higher
strdata = temps(~cellfun('isempty',temps));
%turn string into number
numdata = [];
numdata = cellfun(@str2double,strdata);
%plot
time = 1:length(numdata);
plot(time,numdata)
xlabel('Time (s)')
ylabel('Temp (K)')
  1 Comment
Walter Roberson
Walter Roberson on 9 Aug 2020
temps = regexp(txt, '(\w*2)[\d.]+', 'match');
That does not match only numbers. The \w part matches any number of "word building" characters, which are the digits, the lower-case and upper case letters, the underscore, and a bunch of interational characters such as ªµºÀÁÂÃÄÅÆÇ
The pattern would, for example, match all of ABCD234 . And the str2double() would fail on that.
It would probably make more sense to use '\<[\d.]+'

Sign in to comment.

Accepted Answer

Raunak Gupta
Raunak Gupta on 9 Aug 2020
Hi,
For going values greater than 299K you can change the text according to the range in regexp. For example if you want to cover values from 300-399K also, you can change the regexp expression to something like below.
temps = regexp(txt, '(\w*[2-3])[\d.]+', 'match');
Above expression will find values between 200-399K.
You can follow the expression definition for creating an expression for your case.

More Answers (0)

Tags

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!