Borrar filtros
Borrar filtros

str2num sending figures to printer, is there an alternative?

1 visualización (últimos 30 días)
Jan
Jan el 26 de Mzo. de 2024
Comentada: Jan el 26 de Mzo. de 2024
While parsing a long text file using the STR2NUM command (to extract only numerical values from the file), matlab sent many empty FIGs to the printer for printing. It even used all the papers in the printer! Funny morning. I am aware of the option: Evaluation='restricted', yet it was unexpected.
What happened was that in the text file there was a line of text starting with the word PRINT.
example:
x = str2num('print "Termalization done." append info.txt')
Is there an alternative for simple parsing of text files?
  2 comentarios
Dyuman Joshi
Dyuman Joshi el 26 de Mzo. de 2024
Editada: Dyuman Joshi el 26 de Mzo. de 2024
"to extract only numerical values from the file"
Why are you using str2num for that?
Read the data by using appropriate function e.g. - textscan, fscanf, fileread paired with regexp etc, as per how the data is formatted/structured.
Jan
Jan el 26 de Mzo. de 2024
The textfile format is not fixed and a line of numbers was expected following specific pattenrs. Thus for converting a line of text with a few numbers to reals (vector) the command str2num was a direct option for me. I did not expect my printer to print while converting a string to reals. TEXTSCAN seems like a perfect option. Thank you.

Iniciar sesión para comentar.

Respuestas (1)

Hassaan
Hassaan el 26 de Mzo. de 2024
Editada: Hassaan el 26 de Mzo. de 2024
% Example text line that might be read from your file
textLine = 'The result is 42 and not 43.5 or any other number like -1.23e4';
% Regular expression to match numbers (including decimals and exponentials)
% This pattern matches:
% - Optional sign (+/-)
% - A digit sequence
% - Optional decimal part
% - Optional exponential part (e.g., e+10, e-10)
pattern = '[-+]?\d+\.?\d*(e[-+]?\d+)?';
% Extract matching strings (numbers in this case)
numbersAsStrings = regexp(textLine, pattern, 'match');
% Convert extracted strings to numbers
numbers = str2double(numbersAsStrings);
% Display the extracted numbers
format longG; % This sets the format to long with no trailing zeros
disp(numbers);
42 43.5 -12300
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by