Grab tempretures higher than 299

2 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2020-8-9
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.]+'

请先登录,再进行评论。

采纳的回答

Raunak Gupta
Raunak Gupta 2020-8-9
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

标签

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by