Grab tempretures higher than 299

조회 수: 2 (최근 30일)
Shawntae Harris
Shawntae Harris 2020년 8월 6일
댓글: Walter Roberson 2020년 8월 9일
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에서 Get Started with MATLAB에 대해 자세히 알아보기

태그

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by