text scan question of amplitude and phase

1 view (last 30 days)
fima v
fima v on 20 Jun 2019
Commented: Star Strider on 26 Jun 2019
Hello i have a string which is as follows:
(-7.30313259052058e-002dB,-4.26354774426478e+000°)
how can i extract these two numbers -7.30313259052058e-002 and -4.26354774426478e+000 exactly ?
I would like to get an array 1X2 where the first number is -7.30313259052058e-002 and the second is -4.26354774426478e+000.
I tried text scan %f in many forms its not working.
Thanks

Answers (1)

Star Strider
Star Strider on 20 Jun 2019
Try this:
c = {'-7.30313259052058e-002dB,-4.26354774426478e+000°'};
D = textscan(c{:}, '%fdB%f°', 'Delimiter',',')
producing:
D =
1×2 cell array
{[-73.0313e-003]} {[-4.2635e+000]}
So you need to use the format descriptor string I used, as well as specifying the delimiter.
That worked on the one line you provided, so (barring unforeseen problems), it should work on the rest of your file.
  5 Comments
Star Strider
Star Strider on 21 Jun 2019
This should read your file (and others like it) without problems:
fidi = fopen('testnew1.txt','rt');
firstLine = fgets(fidi); % First Header Line
k1 = 1;
while ~feof(fidi)
StepInfo{k1,1} = fgets(fidi); % ‘Step Information’ Section Header Lines Cell Array
C = textscan(fidi, '%f(%fdB%f°)', 'Delimiter',',', 'CollectOutput',true);
M = cell2mat(C);
if isempty(M) % Empty Matrix Indicates End-Of-File In Case Of Missing ‘End-Of-File’ Indicator
break
end
D{k1,:} = M;
fseek(fidi, 0, 0);
k1 = k1 + 1
end
fclose(fidi);
Out = cell2mat(D); % Optional, You May Want To Keep The Segments Separate
It captures the first line of your file (in ‘firstLine’) so you can use it if necessary, then captures the section header lines (the ‘StepInfo’ cell array), and the individual sections in the ‘D’ cell array. Here, ‘D’ are a (4x1) cell array of (1000x3) double arrays, one for each section.
Star Strider
Star Strider on 26 Jun 2019
I recently encountered a similar situation where someone had a problem running code similar to this, although it ran without error for me. I suggested that the person’s version of MATLAB needed to be updated. Updating it solved the problem, and my code then ran without error for that person as well. If you encounter problems with this code, see 2018b Update 3 for a description of how to download and install the updates.
Note that there are 4 updates for R2018b, and 3 so far for R2019a.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!