Grab tempretures higher than 299

2 visualizaciones (últimos 30 días)
Shawntae Harris
Shawntae Harris el 6 de Ag. de 2020
Comentada: Walter Roberson el 9 de Ag. de 2020
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 comentario
Walter Roberson
Walter Roberson el 9 de Ag. de 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.]+'

Iniciar sesión para comentar.

Respuesta aceptada

Raunak Gupta
Raunak Gupta el 9 de Ag. de 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.

Más respuestas (0)

Categorías

Más información sobre String Parsing en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by