0

Suppose I have a simple text file named "test.txt" of the format below

A=-1.1,2.2,-3.3,4.4B

My intention is to extract data -1.1, 2.2, -3.3 and 4.4 in Matlab from the text file.

How could I do that?

Ps: Note that the data is between a string "A =" and "B" and is separated by a comma.

I managed to extract the first data with the code below.

buffer = fileread('test.txt');
search = 'A=';
local = strfind(buffer, search);
value = sscanf(buffer(local(1,1)+numel(search):end), '%f', 1);

However, I'm not sure how to get the other values from the list that end in the string "B"

2
  • 1
    Are you looking for a function like textscan? It is designed for handling inputs with specific formats like those. For example, you would use something like textscan(string,"%*c %*c %f %f %f %f %*c", 'delimiter', ','). Commented Aug 2, 2020 at 0:01
  • I was not sure how to put the delimiter, thanks @mimocha Commented Aug 2, 2020 at 12:57

1 Answer 1

1

Well, here is my answer:

clear; clc

path = 'test.txt';
fileID = fopen(path, 'r');
A = fscanf(fileID, '%s');
splitStr = regexp(A, '[a-zA-Z]', 'split');
splitStr = regexp(splitStr, '=', 'split');
splitStr = regexp(splitStr{2}, ',', 'split');
disp(splitStr{2})
fclose(fileID);

Output:

'-1.1'    '2.2'    '-3.3'    '4.4'
Sign up to request clarification or add additional context in comments.

1 Comment

While this code may answer the question, providing additional context either as comments with the code or as a separate paragraph regarding how and/or why it solves the problem would improve the answer's long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.